-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_helper.py
executable file
·165 lines (149 loc) · 5.84 KB
/
assignment_helper.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import itertools
import numpy as np
import scipy
import socket
import subprocess
import time
def get_free_port():
sock = socket.socket()
sock.bind(('', 0))
port = sock.getsockname()[1]
sock.close()
return port
def init_qvm_and_quilc(qvm_executable="qvm", quilc_executable="quilc"):
qvm_port = get_free_port()
quilc_port = get_free_port()
qvm_server = subprocess.Popen([qvm_executable, "-S", "-p", str(qvm_port)])
quilc_server = subprocess.Popen([quilc_executable, "-R", "-p", str(quilc_port)])
fc = ForestConnection(sync_endpoint='http://127.0.0.1:' + str(qvm_port),
compiler_endpoint='tcp://127.0.0.1:' + str(quilc_port))
time.sleep(5)
return qvm_server, quilc_server, fc
def get_amplitudes(circuit):
if isinstance(circuit, qiskit.circuit.QuantumCircuit):
backend = Aer.get_backend('statevector_simulator')
job = execute(circuit, backend)
amplitudes = job.result().get_statevector(circuit)
elif isinstance(circuit, pyquil.quil.Program):
wf_sim = WavefunctionSimulator(connection=fc)
wavefunction = wf_sim.wavefunction(circuit)
amplitudes = wavefunction.amplitudes
else:
raise ValueError("Unknown circuit type")
return amplitudes
def get_counts(circuit, num_shots=100):
if isinstance(circuit, qiskit.circuit.quantumcircuit.QuantumCircuit):
backend = Aer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots=num_shots)
result = job.result()
counts = result.get_counts(circuit)
elif isinstance(circuit, pyquil.quil.Program):
n_qubits = len(circuit.get_qubits())
circuit.wrap_in_numshots_loop(num_shots)
qc = get_qc(str(n_qubits) + 'q-qvm', connection=fc)
executable = qc.compile(circuit)
result = qc.run(executable)
classical_bits = get_classical_bits(circuit)
counts = {}
for bitstring in itertools.product(*[{1, 0} for _ in range(classical_bits)]):
key = "".join(str(i) for i in bitstring)
value = sum([tuple(d.tolist()) == bitstring for d in result])
counts[key] = value
else:
raise ValueError("Unknown circuit type")
return counts
def get_single_measurement_counts(circuit, num_shots=100):
if isinstance(circuit, qiskit.circuit.quantumcircuit.QuantumCircuit):
backend = Aer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots=num_shots)
result = job.result()
counts = result.get_counts(circuit)
elif isinstance(circuit, pyquil.quil.Program):
n_qubits = len(circuit.get_qubits())
circuit.wrap_in_numshots_loop(num_shots)
qc = get_qc(str(n_qubits) + 'q-qvm', connection=fc)
executable = qc.compile(circuit)
result = qc.run(executable)
classical_bits = get_classical_bits(circuit)
counts = {}
for bitstring in itertools.product(*[{1, 0} for _ in range(classical_bits)]):
key = "".join(str(i) for i in bitstring)
counts[key] = 0
counts["0" * classical_bits] = (result == 0).sum()
counts["0" * (classical_bits-1) + "1"] = (result == 1).sum()
else:
raise ValueError("Unknown circuit type")
return counts
def get_classical_bits(circuit):
if isinstance(circuit, qiskit.circuit.quantumcircuit.QuantumCircuit):
classical_bits = circuit.cregs[0].size
elif isinstance(circuit, pyquil.quil.Program):
for instruction in circuit.instructions:
if isinstance(instruction, pyquil.quilbase.Declare):
classical_bits = instruction.memory_size
break
else:
raise ValueError("Unknown circuit type")
return classical_bits
def get_circuit_length(circuit):
if isinstance(circuit, qiskit.circuit.quantumcircuit.QuantumCircuit):
program_length = sum(circuit.count_ops().values())
elif isinstance(circuit, pyquil.quil.Program):
program_length = len(circuit.instructions)
else:
raise ValueError("Unknown circuit type")
return program_length
if __name__ == "__main__":
try:
import grove
import pyquil
from grove.pyvqe import vqe
from pyquil import Program, get_qc
from pyquil.paulis import PauliSum, PauliTerm, exponential_map, sZ
from pyquil.api import WavefunctionSimulator, ForestConnection
from pyquil.gates import *
try:
qvm_server, quilc_server, fc = init_qvm_and_quilc()
is_forest = True
except FileNotFoundError:
try:
prefix = "/home/local/bin/"
qvm_server, quilc_server, fc = init_qvm_and_quilc(prefix+"qvm",
prefix + "quilc")
is_forest = True
except FileNotFoundError:
is_forest = False
except ImportError:
is_forest = False
try:
import qiskit
import qiskit.aqua
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute
try:
from qiskit import Aer
except ImportError:
from qiskit import BasicAer as Aer
from qiskit.quantum_info import Pauli
from qiskit.aqua.operators import *
is_qiskit = True
except ImportError:
is_qiskit = False
try:
import dimod
import dwave_networkx
import minorminer
is_dwave = True
except ImportError:
is_dwave = False
if not (is_qiskit or is_forest):
raise RuntimeError("No quantum computing framework available!")
if not is_dwave:
raise RuntimeError("D-Wave Ocean is not available!")
print("Available frameworks:")
if is_forest:
print("Forest SDK")
if is_qiskit:
print("Qiskit")
if is_dwave:
print("D-Wave Ocean")