-
Notifications
You must be signed in to change notification settings - Fork 6
/
utility.py
102 lines (82 loc) · 2.43 KB
/
utility.py
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
# Basic utility functions used by all simulation codes.
import random
# Keep track of X and Z errors separately.
class Errors(object):
def __init__(self, x, z):
self.x = x
self.z = z
class ErrorRates(object):
def __init__(self, prep, cnot, meas):
self.prep = prep
self.cnot = cnot
self.meas = meas
# Generate random error(s) on specific qubit(s).
def errorsX1(bit, errors, rate):
if random.random() < rate:
errors.x ^= 1<<bit
def errorsZ1(bit, errors, rate):
if random.random() < rate:
errors.z ^= 1<<bit
def errors1(bit, errors, rate):
r = random.random()
if r < rate:
r = 1 + int(3 * r / rate)
if r&1:
errors.x ^= 1<<bit
if r>>1&1:
errors.z ^= 1<<bit
def errors2(bit1, bit2, errors, rate):
r = random.random()
if r < rate:
r = 1 + int(15 * r / rate)
if r&1:
errors.x ^= 1<<bit1
if r>>1&1:
errors.z ^= 1<<bit1
if r>>2&1:
errors.x ^= 1<<bit2
if r>>3&1:
errors.z ^= 1<<bit2
# Propogation of error, without and with fault.
def cnot0(bit1, bit2, errors):
errors.x ^= (errors.x>>bit1&1)<<bit2
errors.z ^= (errors.z>>bit2&1)<<bit1
def hadamard0(bit, errors):
errxz = (errors.x & (1<<bit)) ^ (errors.z & (1<<bit))
errors.x ^= errxz
errors.z ^= errxz
def cz0(bit1, bit2, errors):
hadamard0(bit2, errors)
cnot0(bit1, bit2, errors)
hadamard0(bit2, errors)
def dualcz0(bit1, bit2, errors):
hadamard0(bit1, errors)
cnot0(bit1, bit2, errors)
hadamard0(bit1, errors)
def prep0(bit, errors):
errors.x ^= errors.x & (1<<bit)
errors.z ^= errors.z & (1<<bit)
def prepZ(bit, errors, errorRates):
prep0(bit, errors)
errorsX1(bit, errors, errorRates.prep)
def prepX(bit, errors, errorRates):
prep0(bit, errors)
errorsZ1(bit, errors, errorRates.prep)
def measZ(bit, errors, errorRates):
errorsX1(bit, errors, errorRates.meas)
return (errors.x>>bit)&1
def measX(bit, errors, errorRates):
errorsZ1(bit, errors, errorRates.meas)
return (errors.z>>bit)&1
def cnot(bit1, bit2, errors, errorRates):
cnot0(bit1, bit2, errors)
errors2(bit1, bit2, errors, errorRates.cnot)
def cz(bit1, bit2, errors, errorRates):
cz0(bit1, bit2, errors)
errors2(bit1, bit2, errors, errorRates.cnot)
def dualcz(bit1, bit2, errors, errorRates):
dualcz0(bit1, bit2, errors)
errors2(bit1, bit2, errors, errorRates.cnot)
def prep(bit, errors, errorRates):
prep0(bit, errors)
errors1(bit, errors, errorRates.prep)