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)
Example #2
0
 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_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 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)