def test_U3(): assert (np.allclose(U3(pi, 0, pi, zero), X(zero))) assert (np.allclose(U3(pi, 0, pi, one), X(one))) assert (np.allclose(U3(pi, 0, pi, s), X(s))) assert (np.allclose(U3(pi, pi / 2, pi / 2, zero), Y(zero))) assert (np.allclose(U3(pi, pi / 2, pi / 2, one), Y(one))) assert (np.allclose(U3(pi, pi / 2, pi / 2, s), Y(s)))
def test_X(): assert (np.array_equal(X(zero), one)) assert (np.array_equal(X(one), zero)) assert (np.array_equal(X(X(zero)), zero)) assert (np.array_equal(X(X(one)), one)) assert (np.array_equal(X(s), np.array([[beta], [alpha]], dtype=np.complex))) assert (np.array_equal(X(X(s)), s))
def cond_phase_shift(circuit): """ Performs the conditional phase shift operation 2|0><0| - I on the given circuit. Parameters ---------- circuit : Circuit Quantum circuit to apply the conditional phase shift operation on Returns ------- state : np.ndarray State of the system after the application of the conditional phase shift operation. """ n = circuit.num_rails state = None # First, invert all qubits for i in range(1, n): qubits = [ i, ] gate = X.copy() circuit.add_gate(gate, qubits) # Then, apply a Z operation on any one qubit, with a control from all other # input qubits. This will ensure that the state gets negated only when it # is |111...1> gate = Z.copy() qubits = [ n - 1, ] controls = range(1, n - 1) circuit.add_gate(gate, qubits, controls) # Finally, invert everything back, so that only |000...0> would have # suffered negation for i in range(1, n): qubits = [ i, ] gate = X.copy() state = circuit.add_gate(gate, qubits) return state
def oracle(circuit, M, targets): """ Constructs the oracle for Grover's search algorithm for a given set of target states. Parameters ---------- circuit : Circuit Quantum circuit to add the oracle to M : integer Number of target states targets : list of strings List of target states (in binary) for the Grover's search algorithm Returns ------- state : np.ndarray State of the system after the application of the oracle. """ state = None n = circuit.num_rails for target in targets: qubits = [n,] # The last qubit of the oracle is flipped when the # function is 1, and is not flipped when the # function is 0 (y XOR f) gate = X.copy() t = np.array(list(target), dtype=int) one_controls = np.where(t == 1)[0] + 1 # +1 since rails are numbered # from 1 to n, not 0 to n-1 zero_controls = -1 * (np.where(t == 0)[0] + 1) controls = np.hstack((one_controls, zero_controls)) state = circuit.add_gate(gate, qubits, controls) return state
def cond_phase_shift(circuit): """ Performs the conditional phase shift operation 2|0><0| - I on the given circuit. Parameters ---------- circuit : Circuit Quantum circuit to apply the conditional phase shift operation on Returns ------- state : np.ndarray State of the system after the application of the conditional phase shift operation. """ n = circuit.num_rails state = None # First, invert all qubits for i in range(1, n): qubits = [i,] gate = X.copy() circuit.add_gate(gate, qubits) # Then, apply a Z operation on any one qubit, with a control from all other # input qubits. This will ensure that the state gets negated only when it # is |111...1> gate = Z.copy() qubits = [n-1,] controls = range(1, n-1) circuit.add_gate(gate, qubits, controls) # Finally, invert everything back, so that only |000...0> would have # suffered negation for i in range(1, n): qubits = [i,] gate = X.copy() state = circuit.add_gate(gate, qubits) return state
def test_Z(): assert (np.allclose(Z(Z(s)), s)) assert (np.allclose(H(Z(H(zero))), X(zero))) assert (np.allclose(H(Z(H(one))), X(one))) assert (np.allclose(H(Z(H(s))), X(s))) assert (np.allclose(H(X(H(zero))), Z(zero))) assert (np.allclose(H(X(H(one))), Z(one))) assert (np.allclose(H(X(H(s))), Z(s)))
def oracle(circuit, M, targets): """ Constructs the oracle for Grover's search algorithm for a given set of target states. Parameters ---------- circuit : Circuit Quantum circuit to add the oracle to M : integer Number of target states targets : list of strings List of target states (in binary) for the Grover's search algorithm Returns ------- state : np.ndarray State of the system after the application of the oracle. """ state = None n = circuit.num_rails for target in targets: qubits = [ n, ] # The last qubit of the oracle is flipped when the # function is 1, and is not flipped when the # function is 0 (y XOR f) gate = X.copy() t = np.array(list(target), dtype=int) one_controls = np.where(t == 1)[0] + 1 # +1 since rails are numbered # from 1 to n, not 0 to n-1 zero_controls = -1 * (np.where(t == 0)[0] + 1) controls = np.hstack((one_controls, zero_controls)) state = circuit.add_gate(gate, qubits, controls) return state