File: qec.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 (281 lines) | stat: -rw-r--r-- 5,521 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* qec.c: Quantum Error Correction

   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 <stdlib.h>

#include "qureg.h"
#include "gates.h"
#include "config.h"
#include "decoherence.h"
#include "measure.h"

/* Type of the QEC. Currently implemented versions are:

   0: no QEC (default)
   1: Steane's 3-bit code */

int type = 0;

/* How many qubits are protected */

int width = 0;


/* Change the status of the QEC. */

void
quantum_qec_set_status(int stype, int swidth)
{
  type = stype;
  width = swidth;
}

/* Get the current QEC status */
 
void
quantum_qec_get_status(int *ptype, int *pwidth)
{
  if(ptype)
    *ptype = type;
  if(pwidth)
    *pwidth = width;
} 

/* Encode a quantum register. All qubits up to SWIDTH are protected,
   the rest is expanded with a repition code. */

void
quantum_qec_encode(int type, int width, quantum_reg *reg)
{
  int i;
  float lambda;

  lambda = quantum_get_decoherence();

  quantum_set_decoherence(0);

  for(i=0;i<reg->width;i++)
    {
      if(i==reg->width-1)
	quantum_set_decoherence(lambda);

      if(i<width)
	{
	  quantum_hadamard(reg->width+i, reg);
	  quantum_hadamard(2*reg->width+i, reg);

	  quantum_cnot(reg->width+i, i, reg);
	  quantum_cnot(2*reg->width+i, i, reg);
	}
      else
	{
	  quantum_cnot(i, reg->width+i, reg);
	  quantum_cnot(i, 2*reg->width+i, reg);
	}
    }

  quantum_qec_set_status(1, reg->width);

  reg->width *= 3;
}

/* Decode a quantum register and perform Quantum Error Correction on
   it */

void
quantum_qec_decode(int type, int width, quantum_reg *reg)
{
  int i, a, b;
  int swidth;
  float lambda;

  lambda = quantum_get_decoherence();

  quantum_set_decoherence(0);

  swidth=reg->width/3;

  quantum_qec_set_status(0, 0);

  for(i=reg->width/3-1;i>=0;i--)
    {
      if(i==0)
	quantum_set_decoherence(lambda);

      if(i<width)
	{
	  quantum_cnot(2*swidth+i, i, reg);
	  quantum_cnot(swidth+i, i, reg);
	  
	  quantum_hadamard(2*swidth+i, reg);
	  quantum_hadamard(swidth+i, reg);
	}
      else
	{
	  quantum_cnot(i, 2*swidth+i, reg);
	  quantum_cnot(i, swidth+i, reg);
	}
    }

  for(i=1;i<=swidth;i++)
    {
      a = quantum_bmeasure(swidth, reg);
      b = quantum_bmeasure(2*swidth-i, reg);
      if(a == 1 && b == 1 && i-1 < width)
	quantum_sigma_z(i-1, reg); /* Z = HXH */
    }
}

/* Counter which can be used to apply QEC periodically */

int
quantum_qec_counter(int inc, int frequency, quantum_reg *reg)
{
  static int counter = 0;
  static int freq = (1<<30);

  if(inc > 0)
    counter += inc;
  else if(inc < 0)
    counter = 0;

  if(frequency > 0)
    freq = frequency;

  if(counter >= freq)
    {
      counter = 0;
      quantum_qec_decode(type, width, reg);
      quantum_qec_encode(type, width, reg);
    }
    
  return counter;
}

/* Fault-tolerant version of the NOT gate */

void
quantum_sigma_x_ft(int target, quantum_reg *reg)
{
  int tmp;
  float lambda;

  tmp = type;
  type = 0;

  lambda = quantum_get_decoherence();
  quantum_set_decoherence(0);

  /* These operations can be performed simultaneously */
  
  quantum_sigma_x(target, reg);
  quantum_sigma_x(target+width, reg);
  quantum_set_decoherence(lambda);
  quantum_sigma_x(target+2*width, reg);

  quantum_qec_counter(1, 0, reg);

  type = tmp;
}

/* Fault-tolerant version of the Controlled NOT gate */

void
quantum_cnot_ft(int control, int target, quantum_reg *reg)
{
  int tmp;
  float lambda;

  tmp = type;
  type = 0;

  /* These operations can be performed simultaneously */
  
  lambda = quantum_get_decoherence();
  quantum_set_decoherence(0);

  quantum_cnot(control, target, reg);
  quantum_cnot(control+width, target+width, reg);
  quantum_set_decoherence(lambda);
  quantum_cnot(control+2*width, target+2*width, reg);

  quantum_qec_counter(1, 0, reg);

  type = tmp;

}

/* Fault-tolerant version of the Toffoli gate */

void
quantum_toffoli_ft(int control1, int control2, int target, quantum_reg *reg)
{
  int i;
  int c1, c2;
  MAX_UNSIGNED mask;

  mask = ((MAX_UNSIGNED) 1 << target)
    + ((MAX_UNSIGNED) 1 << (target+width))
    + ((MAX_UNSIGNED) 1 << (target+2*width));

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

      if(reg->state[i] & ((MAX_UNSIGNED) 1 << control1))
	c1 = 1;
      if(reg->state[i] 
	 & ((MAX_UNSIGNED) 1 << (control1+width)))
	{
	  c1 ^= 1;
	}
      if(reg->state[i] 
	 & ((MAX_UNSIGNED) 1 << (control1+2*width)))
	{
	  c1 ^= 1;
	}

      if(reg->state[i] & ((MAX_UNSIGNED) 1 << control2))
	c2 = 1;
      if(reg->state[i] 
	 & ((MAX_UNSIGNED) 1 << (control2+width)))
	{
	  c2 ^= 1;
	}
      if(reg->state[i] 
	 & ((MAX_UNSIGNED) 1 << (control2+2*width)))
	{
	  c2 ^= 1;
	}

      if(c1 == 1 && c2 == 1)
	reg->state[i] = reg->state[i] ^ mask;

    }

  quantum_decohere(reg);

  quantum_qec_counter(1, 0, reg);

}