示例#1
0
    def test_hermiticity(self):
        n_orbitals = 5

        # Real, no spin
        iop = random_interaction_operator(n_orbitals, real=True)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))

        # Real, spin
        iop = random_interaction_operator(n_orbitals,
                                          expand_spin=True,
                                          real=True)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))

        # Complex, no spin
        iop = random_interaction_operator(n_orbitals, real=False)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))

        # Complex, spin
        iop = random_interaction_operator(n_orbitals,
                                          expand_spin=True,
                                          real=False)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))
示例#2
0
    def test_random_operators_are_reproducible(self):
        op1 = random_diagonal_coulomb_hamiltonian(5, seed=5947)
        op2 = random_diagonal_coulomb_hamiltonian(5, seed=5947)
        numpy.testing.assert_allclose(op1.one_body, op2.one_body)
        numpy.testing.assert_allclose(op1.two_body, op2.two_body)

        op1 = random_interaction_operator(5, seed=8911)
        op2 = random_interaction_operator(5, seed=8911)
        numpy.testing.assert_allclose(op1.one_body_tensor, op2.one_body_tensor)
        numpy.testing.assert_allclose(op1.two_body_tensor, op2.two_body_tensor)

        op1 = random_quadratic_hamiltonian(5, seed=17711)
        op2 = random_quadratic_hamiltonian(5, seed=17711)
        numpy.testing.assert_allclose(op1.combined_hermitian_part,
                                      op2.combined_hermitian_part)
        numpy.testing.assert_allclose(op1.antisymmetric_part,
                                      op2.antisymmetric_part)

        op1 = random_antisymmetric_matrix(5, seed=24074)
        op2 = random_antisymmetric_matrix(5, seed=24074)
        numpy.testing.assert_allclose(op1, op2)

        op1 = random_hermitian_matrix(5, seed=56753)
        op2 = random_hermitian_matrix(5, seed=56753)
        numpy.testing.assert_allclose(op1, op2)

        op1 = random_unitary_matrix(5, seed=56486)
        op2 = random_unitary_matrix(5, seed=56486)
        numpy.testing.assert_allclose(op1, op2)
    def test_hermiticity(self):
        n_qubits = 5

        # Real case
        iop = random_interaction_operator(n_qubits, True)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))

        # Complex case
        iop = random_interaction_operator(n_qubits, False)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))
示例#4
0
    def test_symmetry(self):
        n_orbitals = 5

        # Real.
        iop = random_interaction_operator(n_orbitals,
                                          expand_spin=False,
                                          real=True)
        ferm_op = get_fermion_operator(iop)
        self.assertTrue(is_hermitian(ferm_op))
        two_body_coefficients = iop.two_body_tensor
        for p, q, r, s in itertools.product(range(n_orbitals), repeat=4):

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[r, q, p, s])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[p, s, r, q])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[s, r, q, p])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[q, p, s, r])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[r, s, p, q])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[s, p, q, r])

            self.assertAlmostEqual(two_body_coefficients[p, q, r, s],
                                   two_body_coefficients[q, r, s, p])
示例#5
0
    def test_consistency(self):
        """Test consistency with JW for FermionOperators."""
        # Random interaction operator
        n_qubits = 5
        iop = random_interaction_operator(n_qubits, real=False)
        op1 = jordan_wigner(iop)
        op2 = jordan_wigner(get_fermion_operator(iop))

        self.assertEqual(op1, op2)

        # Interaction operator from molecule
        geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.45))]
        basis = 'sto-3g'
        multiplicity = 1

        filename = os.path.join(DATA_DIRECTORY, 'H1-Li1_sto-3g_singlet_1.45')
        molecule = MolecularData(geometry,
                                 basis,
                                 multiplicity,
                                 filename=filename)
        molecule.load()

        iop = molecule.get_molecular_hamiltonian()
        op1 = jordan_wigner(iop)
        op2 = jordan_wigner(get_fermion_operator(iop))

        self.assertEqual(op1, op2)
示例#6
0
def random_interaction_operator_term(
        order: int,
        real: bool = True,
        seed: Optional[int] = None,
        ) -> openfermion.InteractionOperator:
    """Generates a random interaction operator with non-zero coefficients only
    on terms corresponding to the given number of unique orbitals.

    The number of orbitals is equal to the given order.

    Args:
        order: How many unique orbitals the non-zero terms should correspond to.
        real: Whether or not the coefficients should be real. Defaults to True.
        seed: The seed. If None (default), uses np.random.
    """

    n_orbitals = order

    if order > 4:
        return openfermion.InteractionOperator.zero(order)

    operator = random_interaction_operator(n_orbitals, real=real, seed=seed)
    operator.constant = 0

    for indices in itertools.product(range(n_orbitals), repeat=2):
        if len(set(indices)) != order:
            operator.one_body_tensor[indices] = 0

    for indices in itertools.product(range(n_orbitals), repeat=4):
        if len(set(indices)) != order:
            operator.two_body_tensor[indices] = 0

    return operator
def benchmark_jordan_wigner_sparse(n_qubits):
    """Benchmark the speed at which a FermionOperator is mapped to a matrix.

    Args:
        n_qubits: The number of qubits in the example.

    Returns:
        runtime: The time in seconds that the benchmark took.
    """
    # Initialize a random FermionOperator.
    molecular_operator = random_interaction_operator(n_qubits)
    fermion_operator = get_fermion_operator(molecular_operator)

    # Map to SparseOperator class.
    start_time = time.time()
    sparse_operator = jordan_wigner_sparse(fermion_operator)
    runtime = time.time() - start_time
    return runtime
def benchmark_molecular_operator_jordan_wigner(n_qubits):
    """Test speed with which molecular operators transform to qubit operators.

    Args:
        n_qubits: The size of the molecular operator instance. Ideally, we
            would be able to transform to a qubit operator for 50 qubit
            instances in less than a minute. We are way too slow right now.

    Returns:
        runtime: The number of seconds required to make the conversion.
    """
    # Get an instance of InteractionOperator.
    molecular_operator = random_interaction_operator(n_qubits)

    # Convert to a qubit operator.
    start = time.time()
    qubit_operator = jordan_wigner(molecular_operator)
    end = time.time()

    # Return runtime.
    runtime = end - start
    return runtime
示例#9
0
def test_simulate_trotter_omit_final_swaps():
    n_qubits = 5
    qubits = cirq.LineQubit.range(n_qubits)
    hamiltonian = openfermion.DiagonalCoulombHamiltonian(one_body=numpy.ones(
        (n_qubits, n_qubits)),
                                                         two_body=numpy.ones(
                                                             (n_qubits,
                                                              n_qubits)))
    time = 1.0

    circuit_with_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LINEAR_SWAP_NETWORK))
    circuit_without_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LINEAR_SWAP_NETWORK,
                         omit_final_swaps=True))

    assert (circuit_with_swaps.to_text_diagram(transpose=True).strip() == (
        circuit_without_swaps.to_text_diagram(transpose=True).strip() + """
│        ×ᶠ─────────×ᶠ         ×ᶠ─────────×ᶠ
│        │          │          │          │
×ᶠ───────×ᶠ         ×ᶠ─────────×ᶠ         │
│        │          │          │          │
│        ×ᶠ─────────×ᶠ         ×ᶠ─────────×ᶠ
│        │          │          │          │
×ᶠ───────×ᶠ         ×ᶠ─────────×ᶠ         │
│        │          │          │          │
│        ×ᶠ─────────×ᶠ         ×ᶠ─────────×ᶠ
│        │          │          │          │
""").strip())

    circuit_with_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=1,
                         n_steps=3,
                         algorithm=SPLIT_OPERATOR),
        strategy=cirq.InsertStrategy.NEW)
    circuit_without_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=1,
                         n_steps=3,
                         algorithm=SPLIT_OPERATOR,
                         omit_final_swaps=True),
        strategy=cirq.InsertStrategy.NEW)

    assert (circuit_with_swaps.to_text_diagram(transpose=True).strip() == (
        circuit_without_swaps.to_text_diagram(transpose=True).strip() + """
│         │           │           ×────────────×
│         │           │           │            │
│         ×───────────×           │            │
│         │           │           │            │
│         │           ×───────────×            │
│         │           │           │            │
×─────────×           │           │            │
│         │           │           │            │
│         │           │           ×────────────×
│         │           │           │            │
│         ×───────────×           │            │
│         │           │           │            │
│         │           ×───────────×            │
│         │           │           │            │
×─────────×           │           │            │
│         │           │           │            │
│         │           │           ×────────────×
│         │           │           │            │
│         ×───────────×           │            │
│         │           │           │            │
""").strip())

    hamiltonian = random_interaction_operator(n_qubits, seed=0)
    circuit_with_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LOW_RANK))
    circuit_without_swaps = cirq.Circuit.from_ops(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LOW_RANK,
                         omit_final_swaps=True))

    assert len(circuit_without_swaps) < len(circuit_with_swaps)