File: decoherence.c

package info (click to toggle)
libquantum 1.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,136 kB
  • ctags: 279
  • sloc: sh: 7,711; ansic: 3,849; makefile: 243
file content (130 lines) | stat: -rw-r--r-- 2,827 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* decoherence.c: Simulation of decoherence effects

   Copyright 2003 Bjoern Butscher, Hendrik Weimer

   This file is part of libquantum

   libquantum is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   libquantum is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libquantum; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA

*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "measure.h"
#include "qureg.h"
#include "gates.h"
#include "complex.h"
#include "error.h"

/* Status of the decoherence simulation. Non-zero means enabled and
   decoherence effects will be simulated. */

int quantum_status = 0;

/* Decoherence parameter. The higher the value, the greater the
   decoherence impact. */

float quantum_lambda = 0;

float
quantum_get_decoherence()
{
  return quantum_lambda;
}

/* Initialize the decoherence simulation and set the decoherence
   parameter. */

void 
quantum_set_decoherence(float l)
{
  if(l)
    {
      quantum_status = 1;
      quantum_lambda = l;
    }
  else
    quantum_status = 0;
}

/* Perform the actual decoherence of a quantum register for a single
   step of time. This is done by applying a phase shift by a normal
   distributed angle with the variance LAMBDA. */

void
quantum_decohere(quantum_reg *reg)
{
  float u, v, s, x;
  float *nrands;
  float angle;
  int i, j;

  /* Increase the gate counter */

  quantum_gate_counter(1);

  if(quantum_status)
    {
      
      nrands = calloc(reg->width, sizeof(float));

      if(!nrands)
	quantum_error(QUANTUM_ENOMEM);

      quantum_memman(reg->width * sizeof(float));

      for(i=0; i<reg->width; i++)
	{
	  /* Generate normal distributed random numbers */
	  
     	  do {
	    u = 2 * quantum_frand() - 1;
	    v = 2 * quantum_frand() - 1;
	    s = u * u + v * v;
	  } while (s >= 1);

	  x = u * sqrt(-2 * log(s) / s);

	  x *= sqrt(2 * quantum_lambda);

	  nrands[i] = x/2;
	}

  
      /* Apply the phase shifts for decoherence simulation */

      for(i=0; i<reg->size; i++)
	{
	  angle = 0;

	  for(j=0; j<reg->width; j++)
	    {
	      if(reg->state[i] & ((MAX_UNSIGNED) 1 << j))
		angle += nrands[j];
	      else
		angle -= nrands[j];
	    }

	  reg->amplitude[i] *= quantum_cexp(angle);
	  
	}
      free(nrands);
      quantum_memman(-reg->width * sizeof(float));  
  
    }
}