-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbell_states.py
More file actions
107 lines (83 loc) · 2.82 KB
/
Copy pathbell_states.py
File metadata and controls
107 lines (83 loc) · 2.82 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
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
from qiskit import QuantumCircuit, execute, Aer, IBMQ
# from qiskit.visualization import plot_histogram
# used in Jupyter notebooks
# %config InlineBackend.figure_format = 'svg'
zero_ket = [1, 0]
one_ket = [0, 1]
def bell_states(type='qasm', q0=zero_ket, q1=zero_ket):
qc = create_circuit(type, q0, q1)
draw_circuit(qc)
run_circuit(qc, type)
def create_circuit(type='qasm', q0=zero_ket, q1=zero_ket):
circuit = QuantumCircuit(2)
circuit.initialize(q0, [0])
circuit.initialize(q1, [1])
circuit.h(0)
# circuit.x(1)
# circuit.z(1)
circuit.cx(0, 1)
if type != 'unitary' and type != 'statevector':
circuit.measure_all()
return circuit
def draw_circuit(circuit):
print(circuit.draw())
def get_backend(type='qasm'):
if type == 'quantum':
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q')
# for backend in provider.backends():
# print(backend.status())
return provider.get_backend('ibmq_london')
elif type == 'unitary':
return Aer.get_backend('unitary_simulator')
elif type == 'statevector':
return Aer.get_backend('statevector_simulator')
else:
return Aer.get_backend('qasm_simulator')
def run_circuit(circuit, type='qasm'):
backend = get_backend(type)
if type == 'unitary':
job = execute(circuit, backend)
else:
job = execute(circuit, backend, shots=10, memory=True)
result = job.result()
if type == 'unitary':
print(result.get_unitary())
elif type == 'statevector':
print(result.get_statevector())
else:
print(result.get_counts())
print(result.get_memory())
bell_states(type='statevector', q0=zero_ket, q1=zero_ket)
def explicit_circuit():
circuit = QuantumCircuit()
qubits = QuantumRegister(2, 'qreg')
circuit.add_register(qubits)
bits = ClassicalRegister(2, 'creg')
circuit.add_register(bits)
circuit.h(qubits[0])
circuit.cx(qubits[0], qubits[1])
# circuit.measure(qubits[0], bits[0])
# circuit.measure(qubits[1], bits[1])
circuit.measure(qubits, bits)
print(circuit.draw())
# print(Aer.backends())
# backend = Aer.get_backend('statevector_simulator')
# job = execute(circuit, backend)
# ket = job.result().get_statevector()
# for amplitude in ket:
# print(amplitude)
backend = Aer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots=10, memory=True)
result = job.result()
counts = result.get_counts()
print(counts)
shots = result.get_memory()
print(shots)
# plot_histogram(counts)
# Initialize with arbitrary values
# new_circuit = QuantumCircuit(qubits)
# new_circuit.initialize(ket, qubits)
# new_circuit.h(qubits[0])
# new_circuit.cx(qubits[0], qubits[1])
# print(new_circuit.draw())