def test_simulation_cnot():
    """
    Test if the simulation of CNOT is accurate
    :return:
    """
    prog = Program().inst([H(0), CNOT(0, 1)])
    qvmstab = QVM_Stabilizer(num_qubits=2)
    qvmstab._apply_hadamard(prog.instructions[0])
    qvmstab._apply_cnot(prog.instructions[1])

    # assert that ZZ XX stabilizes a bell state
    true_stabilizers = [sX(0) * sX(1), sZ(0) * sZ(1)]
    test_paulis = binary_stabilizer_to_pauli_stabilizer(qvmstab.tableau[2:, :])
    for idx, term in enumerate(test_paulis):
        assert term == true_stabilizers[idx]

    # test that CNOT does nothing to |00> state
    prog = Program().inst([CNOT(0, 1)])
    qvmstab = QVM_Stabilizer(num_qubits=2)
    qvmstab._apply_cnot(prog.instructions[0])
    true_tableau = np.array([
        [1, 1, 0, 0, 0],  # X1 -> X1 X2
        [0, 1, 0, 0, 0],  # X2 -> X2
        [0, 0, 1, 0, 0],  # Z1 -> Z1
        [0, 0, 1, 1, 0]
    ])  # Z2 -> Z1 Z2

    # note that Z1, Z1 Z2 still stabilizees |00>
    assert np.allclose(true_tableau, qvmstab.tableau)

    # test that CNOT produces 11 state after X
    prog = Program().inst([H(0), S(0), S(0), H(0), CNOT(0, 1)])
    qvmstab = QVM_Stabilizer(num_qubits=2)
    qvmstab._apply_hadamard(prog.instructions[0])
    qvmstab._apply_phase(prog.instructions[1])
    qvmstab._apply_phase(prog.instructions[2])
    qvmstab._apply_hadamard(prog.instructions[3])
    qvmstab._apply_cnot(prog.instructions[4])
    true_tableau = np.array([[1, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 1],
                             [0, 0, 1, 1, 0]])

    # note that -Z1, Z1 Z2 still stabilizees |11>
    assert np.allclose(true_tableau, qvmstab.tableau)

    test_paulis = binary_stabilizer_to_pauli_stabilizer(
        qvmstab.stabilizer_tableau())
    state = project_stabilized_state(test_paulis,
                                     qvmstab.num_qubits,
                                     classical_state=[1, 1])
    state_2 = project_stabilized_state(test_paulis, qvmstab.num_qubits)

    assert np.allclose(np.array(state.todense()), np.array(state_2.todense()))
def test_stabilizer_projection_Z():
    """
    test if we project out the correct state
    """
    stabilizer_state = project_stabilized_state([sZ(0)])
    true_state = np.zeros((2, 1))
    true_state[0, 0] = 1
    assert np.allclose(true_state, stabilizer_state.todense())
def test_stabilizer_projection_ZZ():
    """
    test if we project out the correct state
    """
    stabilizer_state = project_stabilized_state([sZ(0) * sZ(1), sX(0) * sX(1)])
    true_state = np.zeros((4, 1))
    true_state[0, 0] = true_state[3, 0] = 1
    true_state /= np.sqrt(2)
    assert np.allclose(true_state, stabilizer_state.todense())
예제 #4
0
    def wavefunction(self, program):
        """
        Simulate the program and then project out the final state.

        Return the final state as a wavefunction pyquil object

        :param program: pyQuil program composed of stabilizer operations only
        :return: pyquil.Wavefunction.wavefunction object.
        """
        self.load_program(program)
        self.tableau = self._n_qubit_tableau(self.num_qubits)
        self.kernel()
        stabilizers = binary_stabilizer_to_pauli_stabilizer(
            self.stabilizer_tableau())
        stabilizer_state = project_stabilized_state(stabilizers)
        stabilizer_state = stabilizer_state.toarray()
        return Wavefunction(stabilizer_state.flatten())