-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgate_calc.py
42 lines (35 loc) · 1.21 KB
/
gate_calc.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
import numpy as np
import math
import cmath
from functools import reduce
qnum = int(input('number of qubits: '))
def gate_scale(gate, ap_qubit):
matrix_list = []
dimensions = int(math.sqrt(np.size(gate)))
identity_matrix = np.identity(dimensions, np.matrix)
#generate list
if ap_qubit == 'all':
for i in range(qnum):
matrix_list.append(gate)
else:
ap_qubit = int(ap_qubit)
for i in range(qnum):
matrix_list.append(identity_matrix)
matrix_list[ap_qubit-1] = gate
#iterate through list
return reduce(np.kron, matrix_list)
def save_gate(matrix):
matrix_name = input('please input a name for your matrix: ')
np.savetxt(matrix_name+'.txt', matrix, delimiter=',')
dimension = int(input('size of matrix to scale: '))
value_hold = []
for y in range(dimension):
for x in range(dimension):
element = input('What value for position ({}, {}): '.format(y+1, x+1))
element.strip("\"\'\\\/") #to sanitize input
value_hold.append(eval(element))
matrix = np.matrix(np.resize(value_hold, (dimension, dimension)))
ap_qubit = input('qubit to apply to: ')
matrix = gate_scale(matrix, ap_qubit)
print(matrix)
save_gate(matrix)