forked from ernchern/qiskit-vaqsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircuit.py
86 lines (61 loc) · 2.02 KB
/
circuit.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
import qiskit
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
def build_circuit(params, a_1, b_1, a_2, b_2, shots = 1000, verbose = False):
'''
Inputs:
measure_index: index of a qubit to measure
params: numpy array of [theta, phi, lam]
shots: number of executions of the circuit
a_1:
b_1:
a_2:
b_2:
Output:
out_counts: numpy array of probabilities of outcomes in the order: '00', '01', '10', '11'
'''
# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')
# Create a Quantum Circuit acting on the q register
circuit1 = QuantumCircuit(1, 1)
if b_1!=0:
circuit1.u3(2*np.arccos(a_1),np.arccos(np.real(b_1)/abs(b_1)),0,0)
else:
circuit1.u3(2*np.arccos(a_1),0,0,0)
circuit1.u3(params[0],params[1],params[2],0)
circuit1.measure([0], [0])
# Execute the circuit on the qasm simulator
job1 = execute(circuit1, simulator, shots = shots)
# Grab results from the job
result1 = job1.result()
# Returns counts
counts1 = result1.get_counts(circuit1)
# Create a Quantum Circuit acting on the q register
circuit2 = QuantumCircuit(1, 1)
#circuit2.h(0)
if b_2!=0:
circuit2.u3(2*np.arccos(a_2),np.arccos(np.real(b_2)/abs(b_2)),0,0)
else:
circuit2.u3(2*np.arccos(a_2),0,0,0)
circuit2.u3(params[0],params[1],params[2],0)
circuit2.measure([0], [0])
# Execute the circuit on the qasm simulator
job2 = execute(circuit2, simulator, shots = shots)
# Grab results from the job
result2 = job2.result()
# Returns counts
counts2 = result2.get_counts(circuit2)
if verbose:
print(circuit1)
print(circuit2)
for i in ['0', '1']:
if not i in counts1:
counts1[i] = 0
for i in ['0', '1']:
if not i in counts2:
counts2[i] = 0
out_prob = (counts1['0']+counts2['1'])/(2*shots)
return (out_prob, 0)