1
1
import numpy as np
2
2
import math
3
3
import cmath
4
+ from functools import reduce
4
5
5
6
qnum = int (input ('number of qubits: ' ))
6
7
7
- def gate_scale (gate , ap_qubit ):#this does not work
8
+ def gate_scale (gate , ap_qubit ):
9
+ matrix_list = []
8
10
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 )
12
17
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 )
28
25
29
26
def save_gate (matrix ):
30
27
matrix_name = input ('please input a name for your matrix: ' )
@@ -39,7 +36,7 @@ def save_gate(matrix):
39
36
element .strip ("\" \' \\ \/" ) #to sanitize input
40
37
value_hold .append (eval (element ))
41
38
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: ' )
43
40
matrix = gate_scale (matrix , ap_qubit )
44
41
print (matrix )
45
42
save_gate (matrix )
0 commit comments