Esempio n. 1
0
 def test_qubits_6_py_lih_cct(self):
     """ qubits 6 py lih cct test """
     hrfo = HartreeFock(6, 10, [1, 1], 'parity', True, [1, 2])
     cct = hrfo.construct_circuit('circuit')
     self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[6];\n'
                                  'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n'
                                  'u3(3.14159265358979,0.0,3.14159265358979) q[1];\n')
Esempio n. 2
0
    def test_hf_value(self, mapping):
        try:
            driver = PySCFDriver(atom='Li .0 .0 .0; H .0 .0 1.6',
                                 unit=UnitsType.ANGSTROM,
                                 charge=0,
                                 spin=0,
                                 basis='sto3g')
        except QiskitChemistryError:
            self.skipTest('PYSCF driver does not appear to be installed')
        qmolecule = driver.run()
        core = Hamiltonian(transformation=TransformationType.FULL,
                           qubit_mapping=mapping,
                           two_qubit_reduction=False,
                           freeze_core=False,
                           orbital_reduction=[])

        qubit_op, _ = core.run(qmolecule)
        qubit_op = op_converter.to_matrix_operator(qubit_op)
        hf = HartreeFock(qubit_op.num_qubits,
                         core.molecule_info['num_orbitals'],
                         core.molecule_info['num_particles'], mapping.value,
                         False)
        qc = hf.construct_circuit('vector')
        hf_energy = qubit_op.evaluate_with_statevector(
            qc)[0].real + core._nuclear_repulsion_energy

        self.assertAlmostEqual(qmolecule.hf_energy, hf_energy, places=8)
Esempio n. 3
0
class TestInitialStateHartreeFock(QiskitChemistryTestCase):
    def test_qubits_4_jw_h2(self):
        self.hf = HartreeFock(4, 4, 2, 'jordan_wigner', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_4_py_h2(self):
        self.hf = HartreeFock(4, 4, 2, 'parity', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_4_bk_h2(self):
        self.hf = HartreeFock(4, 4, 2, 'bravyi_kitaev', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_2_py_h2(self):
        self.hf = HartreeFock(2, 4, 2, 'parity', True)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [0.0, 1.0, 0.0, 0.0])

    def test_qubits_2_py_h2_cct(self):
        self.hf = HartreeFock(2, 4, 2, 'parity', True)
        cct = self.hf.construct_circuit('circuit')
        self.assertEqual(
            cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n')

    def test_qubits_6_py_lih_cct(self):
        self.hf = HartreeFock(6, 10, 2, 'parity', True, [1, 2])
        cct = self.hf.construct_circuit('circuit')
        self.assertEqual(
            cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[6];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[1];\n')
Esempio n. 4
0
class TestInitialStateHartreeFock(QiskitChemistryTestCase):
    def test_qubits_4_jw_h2(self):
        self.hf = HartreeFock(4, 4, [1, 1], 'jordan_wigner', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_4_py_h2(self):
        self.hf = HartreeFock(4, 4, [1, 1], 'parity', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_4_bk_h2(self):
        self.hf = HartreeFock(4, 4, [1, 1], 'bravyi_kitaev', False)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0
        ])

    def test_qubits_2_py_h2(self):
        self.hf = HartreeFock(2, 4, 2, 'parity', True)
        cct = self.hf.construct_circuit('vector')
        np.testing.assert_array_equal(cct, [0.0, 1.0, 0.0, 0.0])

    def test_qubits_2_py_h2_cct(self):
        self.hf = HartreeFock(2, 4, [1, 1], 'parity', True)
        cct = self.hf.construct_circuit('circuit')
        self.assertEqual(
            cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n')

    def test_qubits_6_py_lih_cct(self):
        self.hf = HartreeFock(6, 10, [1, 1], 'parity', True, [1, 2])
        cct = self.hf.construct_circuit('circuit')
        self.assertEqual(
            cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[6];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n'
            'u3(3.14159265358979,0.0,3.14159265358979) q[1];\n')

    def test_qubits_10_bk_lih_bitstr(self):
        self.hf = HartreeFock(10, 10, [1, 1], 'bravyi_kitaev', False)
        bitstr = self.hf.bitstr
        np.testing.assert_array_equal(
            bitstr,
            [False, False, False, False, True, False, True, False, True, True])

    @parameterized.expand([[QubitMappingType.JORDAN_WIGNER],
                           [QubitMappingType.PARITY],
                           [QubitMappingType.BRAVYI_KITAEV]])
    def test_hf_value(self, mapping):
        try:
            driver = PySCFDriver(atom='Li .0 .0 .0; H .0 .0 1.6',
                                 unit=UnitsType.ANGSTROM,
                                 charge=0,
                                 spin=0,
                                 basis='sto3g')
        except QiskitChemistryError:
            self.skipTest('PYSCF driver does not appear to be installed')
        qmolecule = driver.run()
        core = Hamiltonian(transformation=TransformationType.FULL,
                           qubit_mapping=mapping,
                           two_qubit_reduction=False,
                           freeze_core=False,
                           orbital_reduction=[])

        qubit_op, _ = core.run(qmolecule)
        qubit_op = op_converter.to_matrix_operator(qubit_op)
        hf = HartreeFock(qubit_op.num_qubits,
                         core.molecule_info['num_orbitals'],
                         core.molecule_info['num_particles'], mapping.value,
                         False)
        qc = hf.construct_circuit('vector')
        hf_energy = qubit_op.evaluate_with_statevector(
            qc)[0].real + core._nuclear_repulsion_energy

        self.assertAlmostEqual(qmolecule.hf_energy, hf_energy, places=8)
Esempio n. 5
0
 def test_qubits_2_py_h2_cct(self):
     """ qubits 2 py h2 cct test """
     hrfo = HartreeFock(2, 4, [1, 1], 'parity', True)
     cct = hrfo.construct_circuit('circuit')
     self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n'
                                  'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n')
Esempio n. 6
0
 def test_qubits_2_py_h2(self):
     """ qubits 2 py h2 test """
     hrfo = HartreeFock(2, 4, 2, 'parity', True)
     cct = hrfo.construct_circuit('vector')
     np.testing.assert_array_equal(cct, [0.0, 1.0, 0.0, 0.0])
Esempio n. 7
0
 def test_qubits_4_bk_h2(self):
     """ qubits 4 bk h2 test """
     hrfo = HartreeFock(4, 4, [1, 1], 'bravyi_kitaev', False)
     cct = hrfo.construct_circuit('vector')
     np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                                         0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
Esempio n. 8
0
 def test_qubits_4_py_h2(self):
     """ qubits 4 py h2 test """
     hrfo = HartreeFock(4, 4, [1, 1], 'parity', False)
     cct = hrfo.construct_circuit('vector')
     np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
                                         0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
Esempio n. 9
0
 def test_qubits_4_jw_h2(self):
     """ qubits 4 jw h2 test """
     hrfo = HartreeFock(4, 4, [1, 1], 'jordan_wigner', False)
     cct = hrfo.construct_circuit('vector')
     np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
                                         0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])