-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.py
More file actions
72 lines (52 loc) · 1.9 KB
/
Copy pathcheck.py
File metadata and controls
72 lines (52 loc) · 1.9 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
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
from qiskit import *
from qiskit.quantum_info import Statevector, DensityMatrix, partial_trace, random_statevector
from qiskit.quantum_info.operators import Operator
from qiskit.circuit.library import XGate, SGate, SdgGate, CPhaseGate
from qiskit.extensions import UnitaryGate
from qasm_parser import importQasm, exportQasm
import builtins as __builtin__
import numpy as np
import random
import sys
random.seed(0)
origin_name = sys.argv[1]
rebuild_name = sys.argv[2]
parse = True
def measure(qc, shots=1000):
aer_sim = Aer.get_backend('aer_simulator')
transpiled = transpile(qc, aer_sim)
qobj = assemble(transpiled)
results = aer_sim.run(qobj, shots=shots).result()
counts = results.get_counts()
return counts
parse = True
if parse:
origin_c = origin_name
qc = importQasm(origin_c)
output_c = rebuild_name
o_qc = importQasm(output_c)
else:
origin_c = origin_name
output_c = rebuild_name
c = open(origin_c, "r")
qasm = c.read()
qc = QuantumCircuit.from_qasm_str(qasm)
o = open(output_c, "r")
o_qasm = o.read()
o_qc = QuantumCircuit.from_qasm_str(o_qasm)
shot = 1000
print("shot:", shot)
result = measure(qc, shots=shot)
o_result = measure(o_qc, shots=shot)
def calculate_probability_distribution(counts, shots):
return {key: value / shots for key, value in counts.items()}
result_probs = calculate_probability_distribution(result, shot)
o_result_probs = calculate_probability_distribution(o_result, shot)
all_keys = set(result_probs.keys()).union(o_result_probs.keys())
result_vector = np.array([result_probs.get(key, 0) for key in all_keys])
o_result_vector = np.array([o_result_probs.get(key, 0) for key in all_keys])
similarity = np.sum(np.sqrt(result_vector * o_result_vector))
print("Similarity:", similarity)