forked from sola-st/MorphQ-Quantum-Qiskit-Testing-ICSE-23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_platform_interface.py
82 lines (63 loc) · 2.81 KB
/
multi_platform_interface.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
"""Interface to run quantum circuits on different platforms.
It supports conversion from qasm to the platform-specific circuits and it
can also run them and return the result.
Some platforms supported are:
- cirq
- qiskit
"""
import qiskit
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, execute, Aer
import cirq
from cirq.contrib.qasm_import import circuit_from_qasm
from cProfile import run
from functools import reduce
import re
from typing import List, Dict, FrozenSet, cast, Any
def convert_qasm_to_cirq_native(qasm_path: str) -> cirq.circuits.Circuit:
"""Convert qasm file path to a cirq circuit object."""
return circuit_from_qasm(open(qasm_path, 'r').read())
def convert_qasm_to_qiskit_native(qasm_path: str) -> QuantumCircuit:
"""Convert qasm file path to a qiskit circuit object."""
return QuantumCircuit.from_qasm_file(qasm_path)
def run_cirq(
qc_cirq: cirq.circuits.circuit.Circuit,
shots: int = 8192):
"""Run cirq and return the result."""
simulator = cirq.Simulator()
result = simulator.run(qc_cirq, repetitions=shots)
measurement_keys = qc_cirq.all_measurement_key_names()
register_numbers = [
#int(re.findall(r'\[(\d+)\]', measurement_key)[0])
int(re.search(r"\d+", measurement_key).group(0))
for measurement_key in measurement_keys
]
sorted_measurement_keys = list(zip(*sorted(zip(register_numbers, measurement_keys))))[1]
result_dict = dict(result.multi_measurement_histogram(keys=sorted_measurement_keys))
keys = list(map(lambda arr: reduce(lambda x, y: str(x) + str(y), arr[::-1]), result_dict.keys()))
counts_cirq = dict(zip(keys, [value for value in result_dict.values()]))
return counts_cirq
def run_qiskit(
qc_qiskit: qiskit.circuit.quantumcircuit.QuantumCircuit,
shots: int = 8192):
"""Run qiskit and return the result."""
backend = Aer.get_backend('qasm_simulator')
job = execute(qc_qiskit, backend=backend, shots=shots)
job_result = job.result()
counts_qiskit = job_result.get_counts(qc_qiskit)
return counts_qiskit
def execute_qiskit_and_cirq(
qc_qiskit: qiskit.circuit.quantumcircuit.QuantumCircuit,
qc_cirq: cirq.circuits.circuit.Circuit,
shots: int = 8192):
"""Execute the quantum circuits and return the result dictionaries."""
counts_qiskit = run_qiskit(qc_qiskit, shots)
counts_cirq = run_cirq(qc_cirq, shots)
return {"qiskit": counts_qiskit, "cirq": counts_cirq}
def convert_and_execute_qiskit_and_cirq_natively(
qasm_path: str,
shots: int = 8192):
"""Convert qasm file to qiskit/cirq circuits and execute them."""
qc_qiskit = convert_qasm_to_qiskit_native(qasm_path)
qc_cirq = convert_qasm_to_cirq_native(qasm_path)
return execute_qiskit_and_cirq(qc_qiskit, qc_cirq, shots)