Skip to content

Commit 520444b

Browse files
committed
fixed, simplified function
1 parent e032633 commit 520444b

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

gate_calc.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
import numpy as np
22
import math
33
import cmath
4+
from functools import reduce
45

56
qnum = int(input('number of qubits: '))
67

7-
def gate_scale(gate, ap_qubit):#this does not work
8+
def gate_scale(gate, ap_qubit):
9+
matrix_list = []
810
dimensions = int(math.sqrt(np.size(gate)))
9-
ap_qubit-=1
10-
if 2**qnum == dimensions:
11-
return gate
11+
identity_matrix = np.identity(dimensions, np.matrix)
12+
13+
#generate list
14+
if ap_qubit == 'all':
15+
for i in range(qnum):
16+
matrix_list.append(gate)
1217
else:
13-
iterator = 1
14-
kron_num = []
15-
identity = np.identity(dimensions, np.matrix)
16-
while iterator <= qnum:#changed from <= dimensions to <= qnum
17-
kron_num.append(identity)
18-
iterator+=1
19-
kron_num[ap_qubit] = gate
20-
kron_iterator = list(range(len(kron_num)))
21-
#has to be a better way to do this chunk of code
22-
for i in kron_iterator:
23-
if i == 0:
24-
x = kron_num[i]
25-
if i > 0:
26-
x = np.kron(x, kron_num[i])
27-
return x
18+
ap_qubit = int(ap_qubit)
19+
for i in range(qnum):
20+
matrix_list.append(identity_matrix)
21+
matrix_list[ap_qubit-1] = gate
22+
23+
#iterate through list
24+
return reduce(np.kron, matrix_list)
2825

2926
def save_gate(matrix):
3027
matrix_name = input('please input a name for your matrix: ')
@@ -39,7 +36,7 @@ def save_gate(matrix):
3936
element.strip("\"\'\\\/") #to sanitize input
4037
value_hold.append(eval(element))
4138
matrix = np.matrix(np.resize(value_hold, (dimension, dimension)))
42-
ap_qubit = int(input('qubit to apply to: '))
39+
ap_qubit = input('qubit to apply to: ')
4340
matrix = gate_scale(matrix, ap_qubit)
4441
print(matrix)
4542
save_gate(matrix)

0 commit comments

Comments
 (0)