forked from openqasm/openqasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scqec.qasm
104 lines (89 loc) · 2.69 KB
/
scqec.qasm
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
/*
* Surface code quantum memory.
*
* Estimate the failure probability as a function
* of parameters at the top of the file.
*/
OPENQASM 3;
include "stdgates.inc";
const int[32] d = 3; // code distance
const int[32] m = 10; // number of syndrome measurement cycles
const int[32] shots = 1000; // number of samples
const int[32] n = d^2; // number of code qubits
uint[32] failures; // number of observed failures
extern zfirst(creg[n - 1], int[32], int[32]);
extern send(creg[n -1 ], int[32], int[32], int[32]);
extern zlast(creg[n], int[32], int[32]) -> bit;
qubit[n] data; // code qubits
qubit[n-1] ancilla; // syndrome qubits
/*
* Ancilla are addressed in a (d-1) by (d-1) square array
* followed by 4 length (d-1)/2 arrays for the top,
* bottom, left, and right boundaries.
*/
bit[n-1] layer; // syndrome outcomes in a cycle
bit[n] data_outcomes; // data outcomes at the end
bit outcome; // logical outcome
/* Declare a sub-circuit for Hadamard gates on ancillas
*/
def hadamard_layer(qubit[n-1] ancilla) {
// Hadamards in the bulk
for row in [0: d-2] {
for col in [0: d-2] {
bit[32] sum = bit[32](row + col);
if(sum[0] == 1)
h ancilla[row * (d - 1) + col];
}
}
// Hadamards on the left and right boundaries
for i in [0: d - 2] {
h ancilla[(d - 1)^2 + (d - 1) + i];
}
}
/* Declare a sub-circuit for a syndrome cycle.
*/
def cycle(qubit[n] data, qubit[n-1] ancilla) -> bit[n-1] {
reset ancilla;
hadamard_layer ancilla;
// First round of CNOTs in the bulk
for row in [0: d - 2] {
for col in [0:d - 2] {
bit[32] sum = bit[32](row + col);
if(sum[0] == 0)
cx data[row * d + col], ancilla[row * (d - 1) + col];
if(sum[0] == 1) {
cx ancilla[row * (d - 1) + col], data[row * d + col];
}
}
}
// First round of CNOTs on the bottom boundary
for i in [0: (d - 3) / 2] {
cx data[d * (d - 1) + 2 * i], ancilla[(d - 1) ^ 2 + ( d- 1) / 2 + i];
}
// First round of CNOTs on the right boundary
for i in [0: (d - 3) / 2] {
cx ancilla[(d - 1) ^ 2 + 3 * (d - 1) / 2 + i], data[2 * d - 1 + 2 * d * i];
}
// Remaining rounds of CNOTs, go here ...
hadamard_layer ancilla;
return measure ancilla;
}
// Loop over shots
for shot in [1: shots] {
// Initialize
reset data;
layer = cycle(data, ancilla);
zfirst(layer, shot, d);
// m cycles of syndrome measurement
for i in [1: m] {
layer = cycle(data, ancilla);
send(layer, shot, i, d);
}
// Measure
data_outcomes = measure data;
outcome = zlast(data_outcomes, shot, d);
failures += int[1](outcome);
}
/* The ratio of "failures" to "shots" is our result.
* The data can be logged by the external functions too.
*/