def test_mapping_for_single_op(self): """Test for single register operator.""" with self.subTest("test +"): op = FermionicOp("+", display_format="dense") expected = PauliSumOp.from_list([("X", 0.5), ("Y", -0.5j)]) self.assertEqual(ParityMapper().map(op), expected) with self.subTest("test -"): op = FermionicOp("-", display_format="dense") expected = PauliSumOp.from_list([("X", 0.5), ("Y", 0.5j)]) self.assertEqual(ParityMapper().map(op), expected) with self.subTest("test N"): op = FermionicOp("N", display_format="dense") expected = PauliSumOp.from_list([("I", 0.5), ("Z", -0.5)]) self.assertEqual(ParityMapper().map(op), expected) with self.subTest("test E"): op = FermionicOp("E", display_format="dense") expected = PauliSumOp.from_list([("I", 0.5), ("Z", 0.5)]) self.assertEqual(ParityMapper().map(op), expected) with self.subTest("test I"): op = FermionicOp("I", display_format="dense") expected = PauliSumOp.from_list([("I", 1)]) self.assertEqual(ParityMapper().map(op), expected)
def test_mapping(self): """Test mapping to qubit operator""" driver = HDF5Driver(hdf5_input=self.get_resource_path( "test_driver_hdf5.hdf5", "second_q/drivers/hdf5d")) driver_result = driver.run() fermionic_op = driver_result.second_q_ops()["ElectronicEnergy"] mapper = ParityMapper() qubit_op = mapper.map(fermionic_op) # Note: The PauliSumOp equals, as used in the test below, use the equals of the # SparsePauliOp which in turn uses np.allclose() to determine equality of # coeffs. So the reference operator above will be matched on that basis so # we don't need to worry about tiny precision changes for any reason. self.assertEqual(qubit_op, TestParityMapper.REF_H2)
def test_two_qubit_reduction_and_z2_symmetry(self): """Test mapping to qubit operator with z2 symmetry tapering and two qubit reduction""" z2_sector = [-1] def cb_finder(z2_symmetries: Z2Symmetries, converter: QubitConverter) -> Optional[List[int]]: return z2_sector if not z2_symmetries.is_empty() else None mapper = ParityMapper() qubit_conv = QubitConverter(mapper, two_qubit_reduction=True, z2symmetry_reduction="auto") qubit_op = qubit_conv.convert(self.h2_op, self.num_particles, sector_locator=cb_finder) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED_TAPER) self.assertEqual(qubit_conv.num_particles, self.num_particles) self.assertListEqual(qubit_conv.z2symmetries.tapering_values, z2_sector) with self.subTest("convert_match()"): qubit_op = qubit_conv.convert_match(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED_TAPER) self.assertEqual(qubit_conv.num_particles, self.num_particles) self.assertListEqual(qubit_conv.z2symmetries.tapering_values, z2_sector) with self.subTest("Change setting"): qubit_conv.z2symmetry_reduction = [1] qubit_op = qubit_conv.convert(self.h2_op, self.num_particles) self.assertNotEqual( qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED_TAPER) qubit_conv.z2symmetry_reduction = [-1] qubit_op = qubit_conv.convert(self.h2_op, self.num_particles) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED_TAPER) with self.subTest("Specify sector upfront"): qubit_conv = QubitConverter(mapper, two_qubit_reduction=True, z2symmetry_reduction=z2_sector) qubit_op = qubit_conv.convert(self.h2_op, self.num_particles) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED_TAPER) with self.subTest("Specify sector upfront, but invalid content"): with self.assertRaises(ValueError): _ = QubitConverter(mapper, two_qubit_reduction=True, z2symmetry_reduction=[5]) with self.subTest("Specify sector upfront, but invalid length"): qubit_conv = QubitConverter(mapper, two_qubit_reduction=True, z2symmetry_reduction=[-1, 1]) with self.assertRaises(QiskitNatureError): _ = qubit_conv.convert(self.h2_op, self.num_particles)
def test_qubits_2_py_h2(self): """qubits 2 py h2 test""" num_particles = (1, 1) converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) converter.force_match(num_particles=num_particles) state = HartreeFock(4, num_particles, converter) ref = QuantumCircuit(2) ref.x(0) self.assertEqual(state, ref)
def test_hf_bitstring_mapped(self): """Mapped bitstring test for water""" # Original driver config when creating operator that resulted in symmetries coded # below. The sector [1, -1] is the correct ground sector. # PySCFDriver( # atom="O 0.0000 0.0000 0.1173; H 0.0000 0.07572 -0.4692;H 0.0000 -0.07572 -0.4692", # unit=UnitsType.ANGSTROM, # charge=0, # spin=0, # basis='sto-3g', # hf_method=HFMethodType.RHF) num_spin_orbitals = 14 num_particles = (5, 5) converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) z2symmetries = Z2Symmetries( symmetries=[Pauli("IZZIIIIZZIII"), Pauli("ZZIZIIZZIZII")], sq_paulis=[Pauli("IIIIIIIIXIII"), Pauli("IIIIIIIIIXII")], sq_list=[3, 2], tapering_values=[1, -1], ) with self.subTest("Matched bitsring creation"): converter.force_match(num_particles=num_particles, z2symmetries=z2symmetries) bitstr = hartree_fock_bitstring_mapped( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, qubit_converter=converter, ) ref_matched = [ True, False, True, True, False, True, False, True, False, False ] self.assertListEqual(bitstr, ref_matched) with self.subTest("Bitsring creation with no tapering"): bitstr = hartree_fock_bitstring_mapped( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, qubit_converter=converter, match_convert=False, ) ref_notaper = [ True, False, True, False, True, True, False, True, False, True, False, False, ] self.assertListEqual(bitstr, ref_notaper)
def test_two_qubit_reduction(self): """Test mapping to qubit operator with two qubit reduction""" mapper = ParityMapper() qubit_conv = QubitConverter(mapper, two_qubit_reduction=True) with self.subTest( "Two qubit reduction ignored as no num particles given"): qubit_op = qubit_conv.convert(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY) self.assertIsNone(qubit_conv.num_particles) with self.subTest("Two qubit reduction, num particles given"): qubit_op = qubit_conv.convert(self.h2_op, self.num_particles) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED) self.assertEqual(qubit_conv.num_particles, self.num_particles) with self.subTest("convert_match()"): qubit_op = qubit_conv.convert_match(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED) self.assertEqual(qubit_conv.num_particles, self.num_particles) with self.subTest("State is reset (Num particles lost)"): qubit_op = qubit_conv.convert(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY) self.assertIsNone(qubit_conv.num_particles) with self.subTest("Num particles given again"): qubit_op = qubit_conv.convert(self.h2_op, self.num_particles) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED) with self.subTest("Set for no two qubit reduction"): qubit_conv.two_qubit_reduction = False self.assertFalse(qubit_conv.two_qubit_reduction) qubit_op = qubit_conv.convert(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY) # Regression test against https://github.com/Qiskit/qiskit-nature/issues/271 with self.subTest( "Two qubit reduction skipped when operator too small"): qubit_conv.two_qubit_reduction = True small_op = FermionicOp([("N_0", 1.0), ("E_1", 1.0)], register_length=2, display_format="sparse") expected_op = 1.0 * (I ^ I) - 0.5 * (I ^ Z) + 0.5 * (Z ^ Z) with contextlib.redirect_stderr(io.StringIO()) as out: qubit_op = qubit_conv.convert(small_op, num_particles=self.num_particles) self.assertEqual(qubit_op, expected_op) self.assertTrue(out.getvalue().strip().startswith( "The original qubit operator only contains 2 qubits! " "Skipping the requested two-qubit reduction!"))
def test_oh_uhf_parity(self): """oh uhf parity test""" driver = PySCFDriver( atom=self.o_h, unit=UnitsType.ANGSTROM, charge=0, spin=1, basis="sto-3g", method=MethodType.UHF, ) result = self._run_driver(driver, converter=QubitConverter(ParityMapper())) self._assert_energy_and_dipole(result, "oh")
def test_oh_rohf_parity_2q(self): """oh rohf parity 2q test""" driver = PySCFDriver( atom=self.o_h, unit=UnitsType.ANGSTROM, charge=0, spin=1, basis="sto-3g", method=MethodType.ROHF, ) result = self._run_driver( driver, converter=QubitConverter(ParityMapper(), two_qubit_reduction=True)) self._assert_energy_and_dipole(result, "oh")
def test_lih_rhf_parity_2q(self): """lih rhf parity 2q test""" driver = PySCFDriver( atom=self.lih, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto-3g", method=MethodType.RHF, ) result = self._run_driver( driver, converter=QubitConverter(ParityMapper(), two_qubit_reduction=True), transformers=[FreezeCoreTransformer()], ) self._assert_energy_and_dipole(result, "lih")
def test_sector_locator_h2o(self): """Test sector locator.""" driver = PySCFDriver( atom="O 0.0000 0.0000 0.1173; H 0.0000 0.07572 -0.4692;H 0.0000 -0.07572 -0.4692", basis="sto-3g", ) es_problem = ElectronicStructureProblem(driver) qubit_conv = QubitConverter( mapper=ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto" ) main_op, _ = es_problem.second_q_ops() qubit_conv.convert( main_op, num_particles=es_problem.num_particles, sector_locator=es_problem.symmetry_sector_locator, ) self.assertListEqual(qubit_conv.z2symmetries.tapering_values, [1, -1])
def test_qubits_6_py_lih(self): """qubits 6 py lih test""" num_particles = (1, 1) converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) z2symmetries = Z2Symmetries( symmetries=[Pauli("ZIZIZIZI"), Pauli("ZZIIZZII")], sq_paulis=[Pauli("IIIIIIXI"), Pauli("IIIIIXII")], sq_list=[2, 3], tapering_values=[1, 1], ) converter.force_match(num_particles=num_particles, z2symmetries=z2symmetries) state = HartreeFock(10, num_particles, converter) ref = QuantumCircuit(6) ref.x([0, 1]) self.assertEqual(state, ref)
def setUp(self): super().setUp() self.driver = PySCFDriver(atom="H 0 0 0.735; H 0 0 0", basis="631g") self.qubit_converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) self.electronic_structure_problem = ElectronicStructureProblem( self.driver, [FreezeCoreTransformer()] ) self.num_spin_orbitals = 8 self.num_particles = (1, 1) # because we create the initial state and ansatzes early, we need to ensure the qubit # converter already ran such that convert_match works as expected main_op, _ = self.electronic_structure_problem.second_q_ops() _ = self.qubit_converter.convert( main_op, self.num_particles, ) self.reference_energy_pUCCD = -1.1434447924298028 self.reference_energy_UCCD0 = -1.1476045878481704 self.reference_energy_UCCD0full = -1.1515491334334347 # reference energy of UCCSD/VQE with tapering everywhere self.reference_energy_UCCSD = -1.1516142309717594 # reference energy of UCCSD/VQE when no tapering on excitations is used self.reference_energy_UCCSD_no_tap_exc = -1.1516142309717594 # excitations for succ self.reference_singlet_double_excitations = [ [0, 1, 4, 5], [0, 1, 4, 6], [0, 1, 4, 7], [0, 2, 4, 6], [0, 2, 4, 7], [0, 3, 4, 7], ] # groups for succ_full self.reference_singlet_groups = [ [[0, 1, 4, 5]], [[0, 1, 4, 6], [0, 2, 4, 5]], [[0, 1, 4, 7], [0, 3, 4, 5]], [[0, 2, 4, 6]], [[0, 2, 4, 7], [0, 3, 4, 6]], [[0, 3, 4, 7]], ]
def setUp(self): super().setUp() algorithm_globals.random_seed = 42 driver = HDF5Driver(hdf5_input=self.get_resource_path( "test_driver_hdf5.hdf5", "second_q/drivers/hdf5d")) problem = ElectronicStructureProblem(driver) main_op, aux_ops = problem.second_q_ops() converter = QubitConverter(mapper=ParityMapper(), two_qubit_reduction=True) num_particles = ( problem.grouped_property_transformed.get_property( "ParticleNumber").num_alpha, problem.grouped_property_transformed.get_property( "ParticleNumber").num_beta, ) self.qubit_op = converter.convert(main_op, num_particles) self.aux_ops = converter.convert_match(aux_ops) self.reference_energy = -1.857275027031588
def test_excitation_preserving(self): """Test the excitation preserving wavefunction on a chemistry example.""" driver = HDF5Driver( self.get_resource_path("test_driver_hdf5.hdf5", "second_q/drivers/hdf5d")) converter = QubitConverter(ParityMapper()) problem = ElectronicStructureProblem(driver) _ = problem.second_q_ops() particle_number = cast( ParticleNumber, problem.grouped_property_transformed.get_property(ParticleNumber)) num_particles = (particle_number.num_alpha, particle_number.num_beta) num_spin_orbitals = particle_number.num_spin_orbitals optimizer = SLSQP(maxiter=100) initial_state = HartreeFock(num_spin_orbitals, num_particles, converter) wavefunction = ExcitationPreserving(num_spin_orbitals) wavefunction.compose(initial_state, front=True, inplace=True) solver = VQE( ansatz=wavefunction, optimizer=optimizer, quantum_instance=QuantumInstance( BasicAer.get_backend("statevector_simulator"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ), ) gsc = GroundStateEigensolver(converter, solver) result = gsc.solve(problem) self.assertAlmostEqual(result.total_energies[0], self.reference_energy, places=4)
def test_sector_locator_homonuclear(self): """Test sector locator.""" molecule = Molecule( geometry=[("Li", [0.0, 0.0, 0.0]), ("Li", [0.0, 0.0, 2.771])], charge=0, multiplicity=1 ) freeze_core_transformer = FreezeCoreTransformer(True) driver = ElectronicStructureMoleculeDriver( molecule, basis="sto3g", driver_type=ElectronicStructureDriverType.PYSCF ) es_problem = ElectronicStructureProblem(driver, transformers=[freeze_core_transformer]) qubit_conv = QubitConverter( mapper=ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto" ) main_op, _ = es_problem.second_q_ops() qubit_conv.convert( main_op, num_particles=es_problem.num_particles, sector_locator=es_problem.symmetry_sector_locator, ) self.assertListEqual(qubit_conv.z2symmetries.tapering_values, [-1, 1])
def test_freeze_core_z2_symmetry_compatibility(self): """Regression test against #192. An issue arose when the FreezeCoreTransformer was combined with the automatic Z2Symmetry reduction. This regression test ensures that this behavior remains fixed. """ driver = HDF5Driver(hdf5_input=self.get_resource_path( "LiH_sto3g.hdf5", "second_q/transformers")) problem = ElectronicStructureProblem(driver, [FreezeCoreTransformer()]) qubit_converter = QubitConverter( ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto", ) solver = NumPyMinimumEigensolverFactory() gsc = GroundStateEigensolver(qubit_converter, solver) result = gsc.solve(problem) self.assertAlmostEqual(result.total_energies[0], -7.882, places=2)
def test_mapping_basic(self): """Test mapping to qubit operator""" mapper = JordanWignerMapper() qubit_conv = QubitConverter(mapper) qubit_op = qubit_conv.convert(self.h2_op) self.assertIsInstance(qubit_op, PauliSumOp) # Note: The PauliSumOp equals, as used in the test below, use the equals of the # SparsePauliOp which in turn uses np.allclose() to determine equality of # coeffs. So the reference operator above will be matched on that basis so # we don't need to worry about tiny precision changes for any reason. self.assertEqual(qubit_op, TestQubitConverter.REF_H2_JW) with self.subTest("Re-use test"): qubit_op = qubit_conv.convert(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_JW) with self.subTest("convert_match()"): qubit_op = qubit_conv.convert_match(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_JW) with self.subTest("Re-use with different mapper"): qubit_conv.mapper = ParityMapper() qubit_op = qubit_conv.convert(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY) with self.subTest( "Set two qubit reduction - no effect without num particles"): qubit_conv.two_qubit_reduction = True qubit_op = qubit_conv.convert_match(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY) with self.subTest("Force match set num particles"): qubit_conv.force_match(self.num_particles) qubit_op = qubit_conv.convert_match(self.h2_op) self.assertEqual(qubit_op, TestQubitConverter.REF_H2_PARITY_2Q_REDUCED)
def test_uccsd_hf_qasm(self): """uccsd hf test with qasm simulator.""" qubit_converter = QubitConverter(ParityMapper()) ansatz = self._prepare_uccsd_hf(qubit_converter) backend = BasicAer.get_backend("qasm_simulator") optimizer = SPSA(maxiter=200, last_avg=5) solver = VQE( ansatz=ansatz, optimizer=optimizer, expectation=PauliExpectation(), quantum_instance=QuantumInstance( backend=backend, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ), ) gsc = GroundStateEigensolver(qubit_converter, solver) result = gsc.solve(self.electronic_structure_problem) self.assertAlmostEqual(result.total_energies[0], -1.138, places=2)
def test_qubits_4_py_h2(self): """qubits 4 py h2 test""" state = HartreeFock(4, (1, 1), QubitConverter(ParityMapper())) ref = QuantumCircuit(4) ref.x([0, 1]) self.assertEqual(state, ref)
def test_vqe_mes_parity(self): """Test VQEUCCSDFactory with QEOM + Parity mapping""" converter = QubitConverter(ParityMapper()) self._solve_with_vqe_mes(converter)
def test_vqe_mes_parity_2q_auto(self): """Test VQEUCCSDFactory with QEOM + Parity mapping + reduction + auto symmetry""" converter = QubitConverter(ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction="auto") self._solve_with_vqe_mes(converter)
def test_vqe_mes_parity_2q(self): """Test VQEUCCSDFactory with QEOM + Parity mapping + reduction""" converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) self._solve_with_vqe_mes(converter)
def test_allows_two_qubit_reduction(self): """Test this returns True for this mapper""" mapper = ParityMapper() self.assertTrue(mapper.allows_two_qubit_reduction)
def test_build_ucc(self): """Test building UCC""" ucc = UCC() with self.subTest("Check defaulted construction"): self.assertIsNone(ucc.num_particles) self.assertIsNone(ucc.num_spin_orbitals) self.assertIsNone(ucc.excitations) self.assertIsNone(ucc.qubit_converter) self.assertIsNone(ucc.operators) self.assertIsNone(ucc.excitation_list) self.assertEqual(ucc.num_qubits, 0) with self.assertRaises(ValueError): _ = ucc.data with self.subTest("Set num particles"): ucc.num_particles = (1, 1) self.assertEqual(ucc.num_particles, (1, 1)) self.assertIsNone(ucc.operators) with self.assertRaises(ValueError): _ = ucc.data with self.subTest("Set num spin orbitals"): ucc.num_spin_orbitals = 4 self.assertEqual(ucc.num_spin_orbitals, 4) self.assertIsNone(ucc.operators) with self.assertRaises(ValueError): _ = ucc.data with self.subTest("Set excitations"): ucc.excitations = "sd" self.assertEqual(ucc.excitations, "sd") self.assertIsNone(ucc.operators) with self.assertRaises(ValueError): _ = ucc.data with self.subTest("Set qubit converter to complete build"): converter = QubitConverter(JordanWignerMapper()) ucc.qubit_converter = converter self.assertEqual(ucc.qubit_converter, converter) self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 3) self.assertEqual(ucc.num_qubits, 4) self.assertIsNotNone(ucc.data) with self.subTest("Set custom operators"): self.assertEqual(len(ucc.operators), 3) ucc.operators = ucc.operators[:2] self.assertEqual(len(ucc.operators), 2) self.assertEqual(ucc.num_qubits, 4) with self.subTest("Reset operators back to as per UCC"): ucc.operators = None self.assertEqual(ucc.num_qubits, 4) self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 3) with self.subTest("Set num particles to include 0"): ucc.num_particles = (1, 0) self.assertEqual(ucc.num_particles, (1, 0)) self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 1) with self.subTest("Change num particles"): ucc.num_particles = (1, 1) self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 3) with self.subTest("Change num spin orbitals"): ucc.num_spin_orbitals = 6 self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 8) with self.subTest("Change excitations"): ucc.excitations = "s" self.assertIsNotNone(ucc.operators) self.assertEqual(len(ucc.operators), 4) with self.subTest("Change qubit converter"): ucc.qubit_converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) # Has not been used to convert so we need to force it to do two qubit reduction ucc.qubit_converter.force_match(ucc.num_particles) self.assertIsNotNone(ucc.operators) self.assertEqual(ucc.num_qubits, 4)