-
Notifications
You must be signed in to change notification settings - Fork 2
/
circuit_generator.py
154 lines (128 loc) · 4.52 KB
/
circuit_generator.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
import numpy as np
from qiskit import *
from qiskit.visualization import plot_histogram
from qiskit.tools.visualization import circuit_drawer
from qiskit.circuit import Gate
from collections import Counter
import math
from PIL import Image
def apply_gate(circuit,gate_str,applied,ctrl=0):
if gate_str == 'H':
circuit.h(applied)
elif gate_str == 'HZ':
circuit.z(applied)
circuit.h(applied)
elif gate_str == 'X':
circuit.x(applied)
elif gate_str == 'Z':
circuit.z(applied)
elif gate_str == 'CX':
circuit.cx(ctrl,applied)
elif gate_str == 'I':
circuit.iden(applied)
elif gate_str == 'Id':
pass
return circuit
def generate_subcircuit(no_qubits):
gates_lists = ['H', 'HZ', 'X', 'Z', 'CX'] + 3*['Id']
circuit = QuantumCircuit(no_qubits)
all_ids = True
for q in range(no_qubits):
gate = np.random.choice(gates_lists)
if gate != 'Id':
all_ids = False
ctrl = 0
if gate == 'CX':
list_qubits = list(range(no_qubits))
list_qubits.pop(q)
ctrl = np.random.choice(list_qubits)
apply_gate(circuit, gate, q, ctrl)
if not all_ids:
circuit.barrier()
return circuit
#------------------------------------------------------------------------------
#main functions
def generate_game(no_qubits, no_rounds):
Circuits = []
Plays = []
init_circuit = QuantumCircuit(no_qubits)
init_circuit.h(range(no_qubits))
init_circuit.barrier()
Circuits.append(init_circuit)
for n in range(no_rounds):
qubits_play = np.random.choice(list(range(no_qubits)),2,replace = False)
np.random.shuffle(qubits_play)
play = [('P0',qubits_play[0]), ('P1',qubits_play[1])]
Plays.append(play)
Circuits.append(generate_subcircuit(no_qubits))
return Circuits, Plays
def draw_game(Circuits, Plays):
final_circ = Circuits[0].copy()
for play, circ in zip(Plays, Circuits[1:]):
for player, qubit in play:
if player in ['P0','P1']:
final_circ.append(Gate(name = player, num_qubits = 1, params = []),[qubit])
else:
final_circ = apply_gate(final_circ,gate_str = player,applied = qubit)
final_circ.barrier()
final_circ = final_circ + circ
#final_circ.measure()
final_circ.draw(output = 'mpl').savefig('stage.png')
def distribute_cards(no_rounds):
cards_list = ['H', 'HZ', 'X', 'Z', 'I']
deck = np.array(no_rounds*cards_list)
np.random.shuffle(deck)
hand_size = no_rounds + 1
hand_P0 = dict(Counter(deck[0:hand_size]))
hand_P1 = dict(Counter(deck[hand_size:2*hand_size]))
return hand_P0, hand_P1
def play_round(n_round,Plays,P0,P1):
Plays[n_round][0] = (P0, Plays[n_round][0][1])
Plays[n_round][1] = (P1, Plays[n_round][1][1])
return Plays
def get_played_game(Circuits, Plays):
final_circ = Circuits[0].copy()
print(final_circ.draw())
for play, circ in zip(Plays, Circuits[1:]):
for player, qubit in play:
if player in ['P0','P1']:
return final_circ
else:
final_circ = apply_gate(final_circ, gate_str = player,applied = qubit)
final_circ.barrier()
final_circ = final_circ + circ
return final_circ
def compute_state(partial_circ):
backend = Aer.get_backend('statevector_simulator')
job = execute(partial_circ, backend)
result = job.result()
state = result.get_statevector(partial_circ, decimals=3)
return state
def score_counts(state_v):
e_ones = 0
for i, p in enumerate(np.abs(state_v)**2):
e_ones += p*np.sum(np.array(list(bin(i)[2:])).astype(np.int))
return np.around(e_ones/np.log2(len(state_v))*100,decimals = 1)
def state_draw(state):
dict = {}
state = list(state)
tmp = int(math.log(len(state),2))
for i in range(len(state)):
dict[str(bin(i)[2:].zfill(tmp))] = 1000*np.abs(state[i])**2
plot_histogram(dict).savefig("state_prb.png")
def resize_img(title, basewidth):
img = Image.open(title)
if img.size[0] > basewidth:
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save(title)
else:
pass
def resize_img_height(title, base_height):
img = Image.open(title)
if img.size[1] > base_height:
img = img.resize((img.size[0],base_height), Image.ANTIALIAS)
img.save(title)
else:
pass