def create_d_gate(n_qbits): c_n_a_gate = create_controlled_n_a_gate_v1(n_qbits) # print('\nc_n_a_gate') # print(np.array(c_n_a_gate, dtype = float)) x_tensor = apply_n_tensor_to(n_qbits, x) # print('\nx_tensor') # print(np.array(x_tensor, dtype = float)) i_x_tensor = apply_tensor(apply_n_tensor_to(n_qbits - 1, i), x) # print('\ni_x_tensor') # print(np.array(i_x_tensor, dtype = float)) d_gate = x_tensor d_gate = apply_gate_to_psi(c_n_a_gate, d_gate) d_gate = apply_gate_to_psi(i_x_tensor, d_gate) d_gate = apply_gate_to_psi(c_n_a_gate, d_gate) d_gate = apply_gate_to_psi(x_tensor, d_gate) d_gate = apply_gate_to_psi(c_n_a_gate, d_gate) d_gate = apply_gate_to_psi(i_x_tensor, d_gate) d_gate = apply_gate_to_psi(c_n_a_gate, d_gate) return d_gate
def testing_d_gate(n_qbits): n = n_qbits d_gate = create_d_gate(n) print('\nd_gate\n', d_gate) # print(np.array(d_gate, dtype = float)) psi = apply_n_tensor_to(n, qubit_one) print('\ninitial psi') print_psi(psi) psi = apply_gate_to_psi(d_gate, psi) print('\napplying d_gate to psi') print_psi(psi)
def create_controlled_n_a_gate_without_ancilla(n_qbits): """ Description: Make a non unitary O gate Required Params: n_qbits: Number of qbits that algorithm will computer Optional Params: None Return Value: A non unitary O gate Example: """ a_row = a**(1 / 2**(n_qbits - 2)) c_n_a_row_gate = np.identity(4, dtype=complex) c_n_a_row_gate[3][3] = a_row identity_tensor = apply_n_tensor_to(n_qbits - 2, i) identity_c_n_a_row_gate = apply_tensor(identity_tensor, c_n_a_row_gate) controlled_n_not_identity_tensor = apply_tensor( create_controlled_n_not(n_qbits - 2), i) controlled_n_a_gate_without_ancilla = apply_gate_to_psi( identity_c_n_a_row_gate, controlled_n_not_identity_tensor) c_n_1_a_row_gate = np.identity(4, dtype=complex) c_n_1_a_row_gate[3][3] = 1 / a_row identity_c_n_1_a_row_gate = apply_tensor(identity_tensor, c_n_1_a_row_gate) controlled_n_a_gate_without_ancilla = apply_gate_to_psi( identity_c_n_1_a_row_gate, controlled_n_a_gate_without_ancilla) controlled_n_a_gate_without_ancilla = apply_gate_to_psi( controlled_n_not_identity_tensor, controlled_n_a_gate_without_ancilla) matrix_size = 2**n_qbits c_n_a_row_2_gate = np.identity(matrix_size, dtype=complex) c_n_a_row_2_gate[matrix_size - 1][matrix_size - 1] = a_row c_n_a_row_2_gate[matrix_size - 3][matrix_size - 3] = a_row controlled_n_a_gate_without_ancilla = apply_gate_to_psi( c_n_a_row_2_gate, controlled_n_a_gate_without_ancilla) # Returning controlled_n_a_gate_with_ancilla return controlled_n_a_gate_without_ancilla
""" import numpy as np from utils.gates import i, z, h from utils.operations import apply_tensor, apply_n_tensor_to, \ apply_gate_to_psi, print_psi, apply_projective_operator, plot_psi from utils.qbits import qubit_one, qubit_zero ############################################################ ####### Circuit Properties ################################# ############################################################ # Number of qbits that algorithm will computer n = 4 # Item to search: |111....11> item_to_search = apply_n_tensor_to(n + 1, qubit_one) ############################################################ ####### Circuit Gates ###################################### ############################################################ item_to_search_projector = np.dot(item_to_search, np.transpose(item_to_search)) print("\nitem_to_search_projector") print(item_to_search_projector) # Creating Identity"s tensors to works qubits tensor_i = np.identity(2**n, dtype=complex) # Creating oracle oracle = apply_tensor(tensor_i, i) print("\noraculo") print(oracle)
# On the article, in step 1, the oracle is defined as: # u_t = I - 2 * items_to_search_projector # oracle = tensor_i - (2 * items_to_search_projector) # print("\noracle") # print(oracle.real) # Creating qwd. On the article it is defined as # v in step 2 v = create_n_4_qwd_gate(n, m) print("\nv operator") print(v) # Creating Hadamard"s tensors, works tensor_h = apply_n_tensor_to(n, h) print("\ntensor_h") print(tensor_h.real) # Creating controled_u_0 # On the article is: # u_s = 2 * items_to_search_projector - I # controlled_u_0 = 2 * items_to_search_projector - tensor_i controlled_u_0 = apply_tensor(tensor_i, i) matrix_size = len(controlled_u_0) for index in range(matrix_size - 2, matrix_size - 2 - (2 * m), -2): controlled_u_0[index + 1][index + 1] = -1
o_gate_matrix = np.array([ [0, 0], [0, np.power(2, n_qbits)], ], dtype=complex) # Returning o_gate_matrix return o_gate_matrix if __name__ == '__main__': # Number of qbits that algorithm will computer n = 6 # Creating Hadamard's tensors tensor_h = apply_n_tensor_to(n, h) # Creating Fredkin's tensors tensor_f = apply_n_tensor_to(int(n / 3), f) # Creating Indentity's tensors tensor_i = apply_n_tensor_to(n - 1, i) # Creating O operator tensor_o_i = apply_tensor(create_o_gate(n), tensor_i) # psi_0 - Creating tensor product between inputs: |000000> psi = apply_n_tensor_to(n, qubit_zero) print('\npsi_0 - Creating tensor product between inputs: |000000>') print_psi(psi)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0] #15 ], dtype=complex) # Returning uf_gate_matrix return uf_gate_matrix """ <<< MAIN >>> """ if __name__ == '__main__': # Number of qbits that algorithm will computer n = 2 # Creating Hadamard's tensors tensor_h = apply_n_tensor_to(n, h) # Creating Identity's tensors tensor_i = apply_n_tensor_to(n, i) # Creating tensor product between: # Hadamard's tensors and Identity's tensors tensor_h_i = apply_tensor(tensor_h, tensor_i) # Creating Uf gate matrix uf = create_uf_gate(n) # psi_0 - Creating tensor product between inputs: X1 = |0> and X2 = |0> psi = apply_n_tensor_to(2 * n, qubit_zero) # psi_1 - Applying tensor_h_i to psi_0