-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcircuit.py
60 lines (46 loc) · 1.31 KB
/
circuit.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
import matplotlib.pyplot as plt
import numpy as np
from qiskit import *
def ansatz(ansatzList,theta=3.1415):
q = QuantumRegister(2)
c = ClassicalRegister(2)
circuit = QuantumCircuit(q, c)
for gate in ansatzList:
if gate[0]=='H':
p=int(gate[1])
circuit.h(q[p])
if gate[0]=='C':
p0=int(gate[1])
p1=int(gate[2])
circuit.cx(q[p0], q[p1])
"""
if gate[0]=='R':
p=int(gate[1])
circuit.rx(theta, q[p])
"""
if gate[0]=='Y':
p=int(gate[1])
circuit.y(q[p])
if gate[0]=='Z':
p=int(gate[1])
circuit.z(q[p])
if gate[0]=='X':
p=int(gate[1])
circuit.x(q[p])
circuit.measure(q,c)
return circuit
def get_expectation(ansatzList, theta=3.1415):
circuit = ansatz(ansatzList, theta)
shots = 10000
backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots=shots)
result = job.result()
counts = result.get_counts()
return counts
def comparison(ansatzList,theta=3.1415):
estimate=get_expectation(ansatzList,theta)
total=sum(estimate.values())
if '11' in estimate.keys():
return True
else:
return False