def test_ground_state_particle_nonconserving(self):
        """Test getting the ground state preparation circuit for a Hamiltonian
        that does not conserve particle number."""
        for n_qubits in self.n_qubits_range:
            # Initialize a particle-number-conserving Hamiltonian
            quadratic_hamiltonian = random_quadratic_hamiltonian(
                n_qubits, False, True)

            # Compute the true ground state
            sparse_operator = get_sparse_operator(quadratic_hamiltonian)
            ground_energy, _ = get_ground_state(sparse_operator)

            # Obtain the circuit
            circuit_description, start_orbitals = (
                gaussian_state_preparation_circuit(quadratic_hamiltonian))

            # Initialize the starting state
            state = jw_configuration_state(start_orbitals, n_qubits)

            # Apply the circuit
            particle_hole_transformation = (
                jw_sparse_particle_hole_transformation_last_mode(n_qubits))
            for parallel_ops in circuit_description:
                for op in parallel_ops:
                    if op == 'pht':
                        state = particle_hole_transformation.dot(state)
                    else:
                        i, j, theta, phi = op
                        state = jw_sparse_givens_rotation(
                            i, j, theta, phi, n_qubits).dot(state)

            # Check that the state obtained using the circuit is a ground state
            difference = sparse_operator * state - ground_energy * state
            discrepancy = numpy.amax(numpy.abs(difference))
            self.assertAlmostEqual(discrepancy, 0)
Пример #2
0
def _spin_symmetric_gaussian_circuit(
        qubits: Sequence[cirq.Qid],
        quadratic_hamiltonian: 'openfermion.QuadraticHamiltonian',
        occupied_orbitals: Tuple[Sequence[int], Sequence[int]],
        initial_state: Union[int, Sequence[int]]) -> cirq.OP_TREE:

    n_qubits = len(qubits)

    if isinstance(initial_state, int):
        initially_occupied_orbitals = _occupied_orbitals(
            initial_state, n_qubits)
    else:
        initially_occupied_orbitals = initial_state  # type: ignore

    for spin_sector in range(2):
        circuit_description, start_orbitals = (
            gaussian_state_preparation_circuit(quadratic_hamiltonian,
                                               occupied_orbitals[spin_sector],
                                               spin_sector=spin_sector))

        def index_map(i):
            return i + spin_sector * (n_qubits // 2)

        spin_indices = [index_map(i) for i in range(n_qubits // 2)]
        spin_qubits = [qubits[i] for i in spin_indices]

        # Flip bits so that the correct starting orbitals are occupied
        yield (cirq.X(spin_qubits[j]) for j in range(n_qubits // 2)
               if (index_map(j) in initially_occupied_orbitals) != (
                   index_map(j) in [index_map(k) for k in start_orbitals]))

        yield _ops_from_givens_rotations_circuit_description(
            spin_qubits, circuit_description)
def test_not_implemented_spinr_reduced():
    """Tests that currently un-implemented functionality is caught."""
    msg = "Specifying spin sector for non-particle-conserving "
    msg += "Hamiltonians is not yet supported."
    for n_qubits in [2, 4, 6]:
        # Initialize a particle-number-conserving Hamiltonian
        quadratic_hamiltonian = random_quadratic_hamiltonian(
            n_qubits, False, True)

        # Obtain the circuit
        with pytest.raises(NotImplementedError):
            _ = gaussian_state_preparation_circuit(quadratic_hamiltonian,
                                                   spin_sector=1)
Пример #4
0
def _generic_gaussian_circuit(
        qubits: Sequence[cirq.Qid],
        quadratic_hamiltonian: 'openfermion.QuadraticHamiltonian',
        occupied_orbitals: Optional[Sequence[int]],
        initial_state: Union[int, Sequence[int]]) -> cirq.OP_TREE:

    n_qubits = len(qubits)
    circuit_description, start_orbitals = (gaussian_state_preparation_circuit(
        quadratic_hamiltonian, occupied_orbitals))

    if isinstance(initial_state, int):
        initially_occupied_orbitals = _occupied_orbitals(
            initial_state, n_qubits)
    else:
        initially_occupied_orbitals = initial_state  # type: ignore

    # Flip bits so that the correct starting orbitals are occupied
    yield (cirq.X(qubits[j]) for j in range(n_qubits)
           if (j in initially_occupied_orbitals) != (j in start_orbitals))

    yield _ops_from_givens_rotations_circuit_description(
        qubits, circuit_description)
 def test_bad_input(self):
     """Test bad input."""
     with self.assertRaises(ValueError):
         gaussian_state_preparation_circuit('a')