-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqiskit_api.py
More file actions
63 lines (55 loc) · 2.29 KB
/
qiskit_api.py
File metadata and controls
63 lines (55 loc) · 2.29 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
from qiskit import *
from qiskit.circuit import QuantumCircuit
from qiskit.converters import circuit_to_dag
from qiskit.transpiler import TransformationPass
def get_cx_count(f):
circ = QuantumCircuit.from_qasm_file(f)
cops = circ.count_ops()
if 'cx' in cops:
return circ.count_ops()['cx']
else:
return 0
def get_cx_count_fast(f):
with open (f) as file:
s = file.read()
return s.count('cx')
from qiskit.providers.fake_provider import FakePrague
from qiskit import QuantumCircuit, transpile
def get_swap_count(
qasm_file_path,
original_basis_gates,
backend=FakePrague()):
"""
Returns the swap count for the given qasm file.
1. convert the qasm file into a qiskit circuit
2. transpile the circuit onto the hardware topology, using the original basis gates + swap
3. count the number of swaps after transpiling
This SWAP count describes the number of SWAPs that are needed to map the circuit onto the hardware topology.
If we get both swap counts for with or without the compiler,
the difference describes whether the compiler provides advantages even if the hardware topology is considered.
Args:
qasm_file_path (str): Path to the qasm file w.r.t. the current working directory.
original_basis_gates (list): List of basis gates used in the QASM file so that the transpiler does not add complexity.
backend (qiskit.FakeProviderForBackendV2): Backend to transpile the circuit onto. (Default: FakePrague 33 qubits)
Returns:
int: Swap count.
"""
circ = QuantumCircuit.from_qasm_file(qasm_file_path)
basis_gates = None
if 'swap' in original_basis_gates:
print('WARNING: SWAP is already in the basis gates. This might lead to unexpected results.')
else:
basis_gates = original_basis_gates + ['swap']
transpiled_circ = transpile(
circ,
backend=backend,
basis_gates=basis_gates,
seed_transpiler=722,
optimization_level=0,
)
counts = transpiled_circ.count_ops()
return counts.get('swap', 0)
# if __name__ == '__main__':
# qasm_file_path = '/home/dl2276/AllProjects/quicr/optimizer/benchmarks/qaoa_n24_from_python/qaoa_n24_from_python.qasm'
# basis_gates = ['x', 'h', 'rz', 'add', 'cx']
# print(get_swap_count(qasm_file_path, basis_gates))