Example #1
0
    def test_superposition(self):
        x = qc.positive_superposition()
        y1 = (qc.zeros() + qc.ones()) / np.sqrt(2)
        y2 = (qc.zeros() + qc.ones()) * (1 / np.sqrt(2))
        y3 = (1 / np.sqrt(2)) * (qc.ones() + qc.zeros())
        y4 = (1 / np.sqrt(2)) * qc.ones() + qc.zeros() / np.sqrt(2)
        self.assertLess(max_absolute_difference(y1, x), epsilon)
        self.assertLess(max_absolute_difference(y2, x), epsilon)
        self.assertLess(max_absolute_difference(y3, x), epsilon)
        self.assertLess(max_absolute_difference(y4, x), epsilon)

        self.assertLess(
            max_absolute_difference(np.sqrt(2) * x - qc.ones(), qc.zeros()),
            epsilon)
def deutsch_algorithm(f):
    U_f = qc.U_f(f, d=2)
    H = qc.Hadamard()

    phi = H(qc.zeros()) * H(qc.ones())
    phi = U_f(phi)
    phi = H(phi, qubit_indices=[0])

    measurement = phi.measure(qubit_indices=0)
    return measurement
Example #3
0
def deutsch_jozsa_algorithm(d, f):
    # The operators we will need
    U_f = qc.U_f(f, d=d + 1)
    H_d = qc.Hadamard(d)
    H = qc.Hadamard()

    state = qc.zeros(d) * qc.ones(1)
    state = (H_d * H)(state)
    state = U_f(state)
    state = H_d(state, qubit_indices=range(d))

    measurements = state.measure(qubit_indices=range(d))
    return measurements
Example #4
0
def grover_algorithm(d, f):
    # The operators we will need
    Oracle = qc.U_f(f, d=d+1)
    H_d = qc.Hadamard(d)
    H = qc.Hadamard()
    N = 2**d
    zero_projector = np.zeros((N, N))
    zero_projector[0, 0] = 1
    Inversion = H_d((2 * qc.Operator.from_matrix(zero_projector) - qc.Identity(d))(H_d))
    Grover = Inversion(Oracle, qubit_indices=range(d))

    # Initial state
    state = qc.zeros(d) * qc.ones(1)
    state = (H_d * H)(state)

    # Number of Grover iterations
    angle_to_rotate = np.arccos(np.sqrt(1 / N))
    rotation_angle = 2 * np.arcsin(np.sqrt(1 / N))
    iterations = int(round(angle_to_rotate / rotation_angle))
    for i in range(iterations):
        state = Grover(state)

    measurements = state.measure(qubit_indices=range(d))
    return measurements