-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRUS_Conditional.py
More file actions
52 lines (38 loc) · 1.33 KB
/
RUS_Conditional.py
File metadata and controls
52 lines (38 loc) · 1.33 KB
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
from qiskit import *
def get_test_op(qreg,c,circuit,ancilla_reg,conditional):
'''
|000> -> 1/sqrt(2)(|100> + |011>)
'''
if not conditional: # for first iteration
circuit.h(qreg[0])
circuit.cx(qreg[0],qreg[1])
circuit.cx(qreg[1],qreg[2])
circuit.x(qreg[0])
else: # for following iterations
circuit.h(qreg[0]).c_if(c,2) # 2 <= 010 in binary
circuit.cx(qreg[0], qreg[1]).c_if(c,2)
circuit.cx(qreg[1], qreg[2]).c_if(c,2)
circuit.x(qreg[0]).c_if(c,2)
circuit.measure(qreg[ancilla_reg], c[ancilla_reg])
return circuit
def get_test_recovery(qreg,c,circuit,ancilla_reg):
'''
|011> -> |000>
'''
circuit.x(qreg[1]).c_if(c,2)
circuit.x(qreg[2]).c_if(c,2)
return circuit
qr = QuantumRegister(3)
cr = ClassicalRegister(3)
ancilla_reg = 1
circuit = QuantumCircuit(qr,cr)
# apply first iteration
circuit = get_test_op(qr,cr,circuit,ancilla_reg,False)
for i in range(4): # add the recovery+prob circuit multiple times conditionally
circuit.barrier()
circuit = get_test_recovery(qr,cr,circuit,ancilla_reg)
circuit = get_test_op(qr,cr,circuit,ancilla_reg,True)
circuit.draw(output='mpl').show()
simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend=simulator, shots=100).result()
print(result.get_counts())