def test_get_quadratic_hamiltonian_hermitian(self): """Test properly formed quadratic Hamiltonians.""" # Non-particle-number-conserving without chemical potential quadratic_op = get_quadratic_hamiltonian(self.hermitian_op) fermion_operator = get_fermion_operator(quadratic_op) fermion_operator = normal_ordered(fermion_operator) self.assertTrue(normal_ordered(self.hermitian_op) == fermion_operator) # Non-particle-number-conserving chemical potential quadratic_op = get_quadratic_hamiltonian(self.hermitian_op, chemical_potential=3.) fermion_operator = get_fermion_operator(quadratic_op) fermion_operator = normal_ordered(fermion_operator) self.assertTrue(normal_ordered(self.hermitian_op) == fermion_operator) # Particle-number-conserving quadratic_op = get_quadratic_hamiltonian(self.hermitian_op_pc) fermion_operator = get_fermion_operator(quadratic_op) fermion_operator = normal_ordered(fermion_operator) self.assertTrue( normal_ordered(self.hermitian_op_pc) == fermion_operator) fop = FermionOperator('1^ 1') fop *= 0.5E-8 quad_op = get_quadratic_hamiltonian(fop) self.assertEqual(quad_op.constant, 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))
def test_fci_energy(): filename = os.path.join(DATA_DIRECTORY, "H2_sto-3g_singlet_0.7414.hdf5") molecule = MolecularData(filename=filename) reduced_ham = make_reduced_hamiltonian(molecule.get_molecular_hamiltonian(), molecule.n_electrons) numpy_ham = get_number_preserving_sparse_operator( get_fermion_operator(reduced_ham), molecule.n_qubits, num_electrons=molecule.n_electrons, spin_preserving=True) w, _ = numpy.linalg.eigh(numpy_ham.toarray()) assert numpy.isclose(molecule.fci_energy, w[0]) filename = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5") molecule = MolecularData(filename=filename) reduced_ham = make_reduced_hamiltonian(molecule.get_molecular_hamiltonian(), molecule.n_electrons) numpy_ham = get_number_preserving_sparse_operator( get_fermion_operator(reduced_ham), molecule.n_qubits, num_electrons=molecule.n_electrons, spin_preserving=True) w, _ = numpy.linalg.eigh(numpy_ham.toarray()) assert numpy.isclose(molecule.fci_energy, w[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)
def test_mul(self): new_tensor = self.polynomial_tensor_a * self.polynomial_tensor_b self.assertEqual(new_tensor, self.polynomial_tensor_axb) new_tensor_1 = self.polynomial_tensor_a * 2. new_tensor_2 = 2. * self.polynomial_tensor_a self.assertEqual( new_tensor_1, PolynomialTensor({ (): self.constant * 2., (1, 0): self.one_body_a * 2., (1, 1, 0, 0): self.two_body_a * 2. })) self.assertEqual( new_tensor_2, PolynomialTensor({ (): self.constant * 2., (1, 0): self.one_body_a * 2., (1, 1, 0, 0): self.two_body_a * 2. })) self.assertEqual(get_fermion_operator(new_tensor_1), get_fermion_operator(self.polynomial_tensor_a) * 2.) self.assertEqual(get_fermion_operator(new_tensor_2), get_fermion_operator(self.polynomial_tensor_a) * 2.)
def test_random_quadratic(self): n_qubits = 5 quad_ham = random_quadratic_hamiltonian(n_qubits, True) ferm_op = get_fermion_operator(quad_ham) self.assertTrue( normal_ordered(ferm_op) == normal_ordered( get_fermion_operator(get_diagonal_coulomb_hamiltonian( ferm_op))))
def test_multiply(self): n_qubits = 5 op1 = random_diagonal_coulomb_hamiltonian(n_qubits) op2 = op1 * 1.5 op3 = 1.5 * op1 self.assertEqual( get_fermion_operator(op1) * 1.5, get_fermion_operator(op2), get_fermion_operator(op3))
def test_one_body_square_decomposition(self): # Initialize H2 InteractionOperator. n_qubits = 4 filename = os.path.join(THIS_DIRECTORY, 'data', 'H2_sto-3g_singlet_0.7414') molecule = MolecularData(filename=filename) molecule_interaction = molecule.get_molecular_hamiltonian() get_fermion_operator(molecule_interaction) two_body_coefficients = molecule_interaction.two_body_tensor # Decompose. eigenvalues, one_body_squares, _, _ = (low_rank_two_body_decomposition( two_body_coefficients, truncation_threshold=0)) rank = eigenvalues.size for l in range(rank): one_body_operator = FermionOperator() for p, q in itertools.product(range(n_qubits), repeat=2): term = ((p, 1), (q, 0)) coefficient = one_body_squares[l, p, q] one_body_operator += FermionOperator(term, coefficient) one_body_squared = one_body_operator**2 # Get the squared one-body operator via one-body decomposition. if abs(eigenvalues[l]) < 1e-6: with self.assertRaises(ValueError): prepare_one_body_squared_evolution(one_body_squares[l]) continue else: density_density_matrix, basis_transformation_matrix = ( prepare_one_body_squared_evolution(one_body_squares[l])) two_body_operator = FermionOperator() for p, q in itertools.product(range(n_qubits), repeat=2): term = ((p, 1), (p, 0), (q, 1), (q, 0)) coefficient = density_density_matrix[p, q] two_body_operator += FermionOperator(term, coefficient) # Confirm that the rotations diagonalize the one-body squares. hopefully_diagonal = basis_transformation_matrix.dot( numpy.dot( one_body_squares[l], numpy.transpose( numpy.conjugate(basis_transformation_matrix)))) diagonal = numpy.diag(hopefully_diagonal) difference = hopefully_diagonal - numpy.diag(diagonal) self.assertAlmostEqual(0., numpy.amax(numpy.absolute(difference))) density_density_alternative = numpy.outer(diagonal, diagonal) difference = density_density_alternative - density_density_matrix self.assertAlmostEqual(0., numpy.amax(numpy.absolute(difference))) # Test spectra. one_body_squared_spectrum = eigenspectrum(one_body_squared) two_body_spectrum = eigenspectrum(two_body_operator) difference = two_body_spectrum - one_body_squared_spectrum self.assertAlmostEqual(0., numpy.amax(numpy.absolute(difference)))
def test_div(self): new_tensor = self.polynomial_tensor_a / 2. self.assertEqual( new_tensor, PolynomialTensor({ (): self.constant / 2., (1, 0): self.one_body_a / 2., (1, 1, 0, 0): self.two_body_a / 2. })) self.assertEqual(get_fermion_operator(new_tensor), get_fermion_operator(self.polynomial_tensor_a) / 2.)
def test_idiv(self): new_tensor = copy.deepcopy(self.polynomial_tensor_a) new_tensor /= 3. self.assertEqual( new_tensor, PolynomialTensor({ (): self.constant / 3., (1, 0): self.one_body_a / 3., (1, 1, 0, 0): self.two_body_a / 3. })) self.assertEqual(get_fermion_operator(new_tensor), get_fermion_operator(self.polynomial_tensor_a) / 3.)
def test_integration_random_diagonal_coulomb_hamiltonian(self): hamiltonian1 = normal_ordered( get_fermion_operator( random_diagonal_coulomb_hamiltonian(n_qubits=7))) hamiltonian2 = normal_ordered( get_fermion_operator( random_diagonal_coulomb_hamiltonian(n_qubits=7))) reference = normal_ordered(commutator(hamiltonian1, hamiltonian2)) result = commutator_ordered_diagonal_coulomb_with_two_body_operator( hamiltonian1, hamiltonian2) self.assertTrue(result.isclose(reference))
def lih_hamiltonian(): """ Generate test Hamiltonian from LiH. Args: None Return: hamiltonian: FermionicOperator spectrum: List of energies. """ geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.45))] active_space_start = 1 active_space_stop = 3 molecule = MolecularData(geometry, 'sto-3g', 1, description="1.45") molecule.load() molecular_hamiltonian = molecule.get_molecular_hamiltonian( occupied_indices=range(active_space_start), active_indices=range(active_space_start, active_space_stop)) hamiltonian = get_fermion_operator(molecular_hamiltonian) spectrum = eigenspectrum(hamiltonian) return hamiltonian, spectrum
def test_erpa_eom_ham_lih(): filename = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5") molecule = MolecularData(filename=filename) reduced_ham = make_reduced_hamiltonian( molecule.get_molecular_hamiltonian(), molecule.n_electrons) rha_fermion = get_fermion_operator(reduced_ham) permuted_hijkl = np.einsum('ijlk', reduced_ham.two_body_tensor) opdm = np.diag([1] * molecule.n_electrons + [0] * (molecule.n_qubits - molecule.n_electrons)) tpdm = 2 * wedge(opdm, opdm, (1, 1), (1, 1)) rdms = InteractionRDM(opdm, tpdm) dim = 3 # so we don't do the full basis. This would make the test long full_basis = {} # erpa basis. A, B basis in RPA language cnt = 0 # start from 1 to make test shorter for p, q in product(range(1, dim), repeat=2): if p < q: full_basis[(p, q)] = cnt full_basis[(q, p)] = cnt + dim * (dim - 1) // 2 cnt += 1 for rkey in full_basis.keys(): p, q = rkey for ckey in full_basis.keys(): r, s = ckey for sigma, tau in product([0, 1], repeat=2): test = erpa_eom_hamiltonian(permuted_hijkl, tpdm, 2 * q + sigma, 2 * p + sigma, 2 * r + tau, 2 * s + tau).real qp_op = FermionOperator( ((2 * q + sigma, 1), (2 * p + sigma, 0))) rs_op = FermionOperator(((2 * r + tau, 1), (2 * s + tau, 0))) erpa_op = normal_ordered( commutator(qp_op, commutator(rha_fermion, rs_op))) true = rdms.expectation(get_interaction_operator(erpa_op)) assert np.isclose(true, test)
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])
def test_majorana_form(self): """Test getting the Majorana form.""" majorana_matrix, majorana_constant = self.quad_ham_npc.majorana_form() # Convert the Majorana form to a FermionOperator majorana_op = FermionOperator((), majorana_constant) normalization = 1. / numpy.sqrt(2.) for i in range(2 * self.n_qubits): if i < self.n_qubits: left_op = majorana_operator((i, 0), normalization) else: left_op = majorana_operator((i - self.n_qubits, 1), normalization) for j in range(2 * self.n_qubits): if j < self.n_qubits: right_op = majorana_operator( (j, 0), majorana_matrix[i, j] * normalization) else: right_op = majorana_operator( (j - self.n_qubits, 1), majorana_matrix[i, j] * normalization) majorana_op += .5j * left_op * right_op # Get FermionOperator for original Hamiltonian fermion_operator = normal_ordered( get_fermion_operator(self.quad_ham_npc)) self.assertTrue(normal_ordered(majorana_op) == fermion_operator)
def setUp(self): geometry = [('H', (0., 0., 0.)), ('H', (0., 0., 0.7414))] basis = 'sto-3g' multiplicity = 1 filename = os.path.join(DATA_DIRECTORY, 'H2_sto-3g_singlet_0.7414') self.molecule = MolecularData(geometry, basis, multiplicity, filename=filename) self.molecule.load() # Get molecular Hamiltonian. self.molecular_hamiltonian = self.molecule.get_molecular_hamiltonian() # Get FCI RDM. self.fci_rdm = self.molecule.get_molecular_rdm(use_fci=1) # Get explicit coefficients. self.nuclear_repulsion = self.molecular_hamiltonian.constant self.one_body = self.molecular_hamiltonian.one_body_tensor self.two_body = self.molecular_hamiltonian.two_body_tensor # Get fermion Hamiltonian. self.fermion_hamiltonian = normal_ordered( get_fermion_operator(self.molecular_hamiltonian)) # Get qubit Hamiltonian. self.qubit_hamiltonian = jordan_wigner(self.fermion_hamiltonian) # Get the sparse matrix. self.hamiltonian_matrix = get_sparse_operator( self.molecular_hamiltonian)
def test_convert_forward_back(self): n_qubits = 6 random_operator = get_fermion_operator( random_interaction_operator(n_qubits)) chemist_operator = chemist_ordered(random_operator) normalized_chemist = normal_ordered(chemist_operator) difference = normalized_chemist - normal_ordered(random_operator) self.assertAlmostEqual(0., difference.induced_norm())
def test_exception(self): n_qubits = 6 random_operator = get_fermion_operator( random_interaction_operator(n_qubits)) bad_term = ((2, 1), (3, 1)) random_operator += FermionOperator(bad_term) with self.assertRaises(OperatorSpecificationError): chemist_ordered(random_operator)
def test_consistency_for_complex_numbers(self): """Test consistency with JW for FermionOperators.""" # Random interaction operator n_qubits = 8 iop = random_interaction_operator(n_qubits, real=False) op1 = bravyi_kitaev(iop) op2 = bravyi_kitaev(get_fermion_operator(iop)) self.assertEqual(op1, op2)
def test_molecular_operator_consistency(self): # Initialize H2 InteractionOperator. n_qubits = 4 filename = os.path.join(THIS_DIRECTORY, 'data', 'H2_sto-3g_singlet_0.7414') molecule = MolecularData(filename=filename) molecule_interaction = molecule.get_molecular_hamiltonian() molecule_operator = get_fermion_operator(molecule_interaction) constant = molecule_interaction.constant one_body_coefficients = molecule_interaction.one_body_tensor two_body_coefficients = molecule_interaction.two_body_tensor # Perform decomposition. eigenvalues, one_body_squares, one_body_corrections, trunc_error = ( low_rank_two_body_decomposition(two_body_coefficients)) self.assertAlmostEqual(trunc_error, 0.) # Build back operator constant and one-body components. decomposed_operator = FermionOperator((), constant) for p, q in itertools.product(range(n_qubits), repeat=2): term = ((p, 1), (q, 0)) coefficient = (one_body_coefficients[p, q] + one_body_corrections[p, q]) decomposed_operator += FermionOperator(term, coefficient) # Build back two-body component. for l in range(one_body_squares.shape[0]): one_body_operator = FermionOperator() for p, q in itertools.product(range(n_qubits), repeat=2): term = ((p, 1), (q, 0)) coefficient = one_body_squares[l, p, q] if abs(eigenvalues[l]) > 1e-6: self.assertTrue(is_hermitian(one_body_squares[l])) one_body_operator += FermionOperator(term, coefficient) decomposed_operator += eigenvalues[l] * (one_body_operator**2) # Test for consistency. difference = normal_ordered(decomposed_operator - molecule_operator) self.assertAlmostEqual(0., difference.induced_norm()) # Decompose with slightly negative operator that must use eigen molecule = MolecularData(filename=filename) molecule.two_body_integrals[0, 0, 0, 0] -= 1 eigenvalues, one_body_squares, one_body_corrections, trunc_error = ( low_rank_two_body_decomposition(two_body_coefficients)) self.assertAlmostEqual(trunc_error, 0.) # Check for property errors with self.assertRaises(TypeError): eigenvalues, one_body_squares, _, trunc_error = ( low_rank_two_body_decomposition(two_body_coefficients + 0.01j, truncation_threshold=1., final_rank=1))
def test_interaction_operator_mapping(self): # Make sure mapping of FermionOperator to InteractionOperator works. molecular_hamiltonian = get_interaction_operator( self.fermion_hamiltonian) fermion_hamiltonian = get_fermion_operator(molecular_hamiltonian) self.assertTrue(self.fermion_hamiltonian == fermion_hamiltonian) # Make sure mapping of InteractionOperator to QubitOperator works. qubit_hamiltonian = jordan_wigner(self.molecular_hamiltonian) self.assertTrue(self.qubit_hamiltonian == qubit_hamiltonian)
def lih_hamiltonian(): geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.45))] active_space_start = 1 active_space_stop = 3 molecule = MolecularData(geometry, 'sto-3g', 1, description="1.45") molecule.load() molecular_hamiltonian = molecule.get_molecular_hamiltonian( occupied_indices=range(active_space_start), active_indices=range(active_space_start, active_space_stop)) hamiltonian = get_fermion_operator(molecular_hamiltonian) ground_state_energy = eigenspectrum(hamiltonian)[0] return hamiltonian, ground_state_energy
def LiH_sto3g(): """ Generates the Hamiltonian for LiH in the STO-3G basis, at a distance of 1.45 A. """ geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.45))] molecule = MolecularData(geometry, 'sto-3g', 1, description="1.45") molecule.load() molecular_hamiltonian = molecule.get_molecular_hamiltonian() hamiltonian = get_fermion_operator(molecular_hamiltonian) num_electrons = molecule.n_electrons num_orbitals = 2 * molecule.n_orbitals return hamiltonian, num_orbitals, num_electrons
def test_operator_consistency_w_spin(self): # Initialize a random InteractionOperator and FermionOperator. n_orbitals = 3 n_qubits = 2 * n_orbitals random_interaction = random_interaction_operator(n_orbitals, expand_spin=True, real=False, seed=8) random_fermion = get_fermion_operator(random_interaction) # Convert to chemist ordered tensor. one_body_correction, chemist_tensor = \ get_chemist_two_body_coefficients( random_interaction.two_body_tensor, spin_basis=True) # Convert output to FermionOperator. output_operator = FermionOperator((), random_interaction.constant) # Convert one-body. one_body_coefficients = (random_interaction.one_body_tensor + one_body_correction) for p, q in itertools.product(range(n_qubits), repeat=2): term = ((p, 1), (q, 0)) coefficient = one_body_coefficients[p, q] output_operator += FermionOperator(term, coefficient) # Convert two-body. for p, q, r, s in itertools.product(range(n_orbitals), repeat=4): coefficient = chemist_tensor[p, q, r, s] # Mixed spin. term = ((2 * p, 1), (2 * q, 0), (2 * r + 1, 1), (2 * s + 1, 0)) output_operator += FermionOperator(term, coefficient) term = ((2 * p + 1, 1), (2 * q + 1, 0), (2 * r, 1), (2 * s, 0)) output_operator += FermionOperator(term, coefficient) # Same spin. term = ((2 * p, 1), (2 * q, 0), (2 * r, 1), (2 * s, 0)) output_operator += FermionOperator(term, coefficient) term = ((2 * p + 1, 1), (2 * q + 1, 0), (2 * r + 1, 1), (2 * s + 1, 0)) output_operator += FermionOperator(term, coefficient) # Check that difference is small. difference = normal_ordered(random_fermion - output_operator) self.assertAlmostEqual(0., difference.induced_norm())
def test_form(self): n_qubits = 6 random_operator = get_fermion_operator( random_interaction_operator(n_qubits)) chemist_operator = chemist_ordered(random_operator) for term, _ in chemist_operator.terms.items(): if len(term) == 2 or not len(term): pass else: self.assertTrue(term[0][1]) self.assertTrue(term[2][1]) self.assertFalse(term[1][1]) self.assertFalse(term[3][1]) self.assertTrue(term[0][0] > term[2][0]) self.assertTrue(term[1][0] > term[3][0])
def test_hubbard(self): x_dim = 4 y_dim = 5 tunneling = 2. coulomb = 3. chemical_potential = 7. magnetic_field = 11. periodic = False hubbard_model = fermi_hubbard(x_dim, y_dim, tunneling, coulomb, chemical_potential, magnetic_field, periodic) self.assertTrue( normal_ordered(hubbard_model) == normal_ordered( get_fermion_operator( get_diagonal_coulomb_hamiltonian(hubbard_model))))
def test_get_molecular_operator(self): coefficient = 3. operators = ((2, 1), (3, 0), (0, 0), (3, 1)) op = FermionOperator(operators, coefficient) molecular_operator = get_interaction_operator(op) fermion_operator = get_fermion_operator(molecular_operator) fermion_operator = normal_ordered(fermion_operator) self.assertTrue(normal_ordered(op) == fermion_operator) op = FermionOperator('1^ 1') op *= 0.5 * EQ_TOLERANCE molecular_operator = get_interaction_operator(op) self.assertEqual(molecular_operator.constant, 0) self.assertTrue( numpy.allclose(molecular_operator.one_body_tensor, numpy.zeros((2, 2))))
def setUp(self): # Set up molecule. geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.45))] basis = 'sto-3g' multiplicity = 1 filename = os.path.join(THIS_DIRECTORY, 'data', 'H1-Li1_sto-3g_singlet_1.45') self.molecule = MolecularData(geometry, basis, multiplicity, filename=filename) self.molecule.load() # Get molecular Hamiltonian self.molecular_hamiltonian = self.molecule.get_molecular_hamiltonian() self.molecular_hamiltonian_no_core = ( self.molecule.get_molecular_hamiltonian( occupied_indices=[0], active_indices=range(1, self.molecule.n_orbitals))) # Get FCI RDM. self.fci_rdm = self.molecule.get_molecular_rdm(use_fci=1) # Get explicit coefficients. self.nuclear_repulsion = self.molecular_hamiltonian.constant self.one_body = self.molecular_hamiltonian.one_body_tensor self.two_body = self.molecular_hamiltonian.two_body_tensor # Get fermion Hamiltonian. self.fermion_hamiltonian = normal_ordered( get_fermion_operator(self.molecular_hamiltonian)) # Get qubit Hamiltonian. self.qubit_hamiltonian = jordan_wigner(self.fermion_hamiltonian) # Get explicit coefficients. self.nuclear_repulsion = self.molecular_hamiltonian.constant self.one_body = self.molecular_hamiltonian.one_body_tensor self.two_body = self.molecular_hamiltonian.two_body_tensor # Get matrix form. self.hamiltonian_matrix = get_sparse_operator( self.molecular_hamiltonian) self.hamiltonian_matrix_no_core = get_sparse_operator( self.molecular_hamiltonian_no_core)
def setUp(self): # Set up molecule. geometry = [('H', (0., 0., 0.)), ('H', (0., 0., 0.7414))] basis = 'sto-3g' multiplicity = 1 filename = os.path.join(DATA_DIRECTORY, 'H2_sto-3g_singlet_0.7414') molecule = MolecularData(geometry, basis, multiplicity, filename=filename) molecule.load() self.n_fermions = molecule.n_electrons self.n_orbitals = molecule.n_qubits # Get molecular Hamiltonian. molecular_hamiltonian = molecule.get_molecular_hamiltonian() self.fermion_hamiltonian = get_fermion_operator(molecular_hamiltonian)
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() _ = jordan_wigner_sparse(fermion_operator) runtime = time.time() - start_time return runtime