-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbitary_n_gate.py
71 lines (61 loc) · 2.7 KB
/
arbitary_n_gate.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
import numpy as np
import math
from utils import kronecker_delta, gen_binary, hadamard_gate, get_decimal
def cnot_gate_func(row_arr, col_arr):
cnot_matrix = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]]
row_index = get_decimal(row_arr)
col_index = get_decimal(col_arr)
return cnot_matrix[row_index][col_index]
def hadamard_arbitrary_gate(n, qubit_index, dtype=float):
num_dimension = 2 ** n
gate_matrix = np.array(
np.zeros((num_dimension, num_dimension), dtype=dtype))
for row_index in range(len(gate_matrix)):
row_binary = gen_binary(row_index, n)
for col_index in range(len(gate_matrix[row_index])):
col_binary = gen_binary(col_index, n)
state = 1
for comp_index in range(n):
row_split = row_binary[comp_index]
col_split = col_binary[comp_index]
if comp_index == qubit_index:
state *= hadamard_gate(row_split, col_split)
else:
state *= kronecker_delta(row_split, col_split)
gate_matrix[row_index][col_index] = state
return gate_matrix
def apply_arbitrary_hadamard(n, state):
for counter in range(n):
hadamard_gate_to_use = hadamard_arbitrary_gate(n, counter)
state = np.dot(hadamard_gate_to_use, state)
return state
def controlled_gate_arbitrary(n, master_index, slave_index, gate_func):
# Assume that gate_func takes master, slave
num_dimension = 2 ** n
gate_matrix = np.array(
np.zeros((num_dimension, num_dimension), dtype=complex))
for row_index in range(len(gate_matrix)):
row_binary = gen_binary(row_index, n)
for col_index in range(len(gate_matrix[row_index])):
col_binary = gen_binary(col_index, n)
state = 1
control_col_value = [col_binary[master_index],
col_binary[slave_index]]
control_row_value = [row_binary[master_index],
row_binary[slave_index]]
controlled_comp = gate_func(control_row_value, control_col_value)
for comp_index in range(n):
if (comp_index != master_index) and (comp_index != slave_index):
row_split = row_binary[comp_index]
col_split = col_binary[comp_index]
state *= kronecker_delta(row_split, col_split)
state *= controlled_comp
gate_matrix[row_index][col_index] = state
return gate_matrix
def driver():
print(controlled_gate_arbitrary(7, 3, 4, cnot_gate_func))
if __name__ == '__main__':
driver()