def setUp(self): """Setup.""" super().setUp() try: atom = 'H .0 .0 .7414; H .0 .0 .0' pyscf_driver = PySCFDriver(atom=atom, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') self.molecule = pyscf_driver.run() warnings.filterwarnings('ignore', category=DeprecationWarning) core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) warnings.filterwarnings('always', category=DeprecationWarning) qubit_op, _ = core.run(self.molecule) exact_eigensolver = NumPyEigensolver(qubit_op, k=2**qubit_op.num_qubits) result = exact_eigensolver.run() self.reference = result.eigenvalues.real except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed')
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)
def test_h2_two_qubits(self): """Test H2 with parity mapping.""" two_qubit_reduction = True qubit_mapping = 'parity' warnings.filterwarnings('ignore', category=DeprecationWarning) core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=two_qubit_reduction, freeze_core=False, orbital_reduction=[]) warnings.filterwarnings('always', category=DeprecationWarning) qubit_op, _ = core.run(self.molecule) num_orbitals = core.molecule_info['num_orbitals'] num_particles = core.molecule_info['num_particles'] eom_ee = QEomEE(qubit_op, num_orbitals=num_orbitals, num_particles=num_particles, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction) result = eom_ee.run() np.testing.assert_array_almost_equal(self.reference, result['energies'])
def test_swaprz(self): """ SwapRZ variational form test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( qubit_op.num_qubits, operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) var_form = SwapRZ(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, var_form, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) _, result = operator.process_algorithm_result(result) self.assertAlmostEqual(result['energy'], self.reference_energy, places=6)
def test_h2_one_qubit_qasm(self): """Test H2 with tapering and qasm backend""" two_qubit_reduction = True qubit_mapping = 'parity' core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=two_qubit_reduction, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(self.molecule) num_orbitals = core.molecule_info['num_orbitals'] num_particles = core.molecule_info['num_particles'] # tapering z2_symmetries = Z2Symmetries.find_Z2_symmetries(qubit_op) # know the sector tapered_op = z2_symmetries.taper(qubit_op)[1] var_form = RY(tapered_op.num_qubits, depth=1) optimizer = SPSA(max_trials=50) eom_vqe = QEomVQE(tapered_op, var_form, optimizer, num_orbitals=num_orbitals, num_particles=num_particles, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction, z2_symmetries=tapered_op.z2_symmetries, untapered_op=qubit_op) backend = BasicAer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=65536) result = eom_vqe.run(quantum_instance) np.testing.assert_array_almost_equal(self.reference, result['energies'], decimal=2)
def test_h2_two_qubits_statevector(self): """Test H2 with parity mapping and statevector backend.""" two_qubit_reduction = True qubit_mapping = 'parity' core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=two_qubit_reduction, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(self.molecule) num_orbitals = core.molecule_info['num_orbitals'] num_particles = core.molecule_info['num_particles'] initial_state = HartreeFock(qubit_op.num_qubits, num_orbitals=num_orbitals, num_particles=num_particles, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction) var_form = UCCSD(num_qubits=qubit_op.num_qubits, depth=1, num_orbitals=num_orbitals, num_particles=num_particles, initial_state=initial_state, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction) optimizer = COBYLA(maxiter=1000, tol=1e-8) eom_vqe = QEomVQE(qubit_op, var_form, optimizer, num_orbitals=num_orbitals, num_particles=num_particles, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction) backend = BasicAer.get_backend('statevector_simulator') quantum_instance = QuantumInstance(backend) result = eom_vqe.run(quantum_instance) np.testing.assert_array_almost_equal(self.reference, result['energies'], decimal=4)
def test_uccsd_hf(self): """ uccsd hf test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() core = Hamiltonian(qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True) qubit_op, _ = core.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( core.molecule_info['num_orbitals'], core.molecule_info['num_particles'], qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) var_form = UCCSD(num_orbitals=core.molecule_info['num_orbitals'], num_particles=core.molecule_info['num_particles'], initial_state=initial_state, qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) algo = VQE(qubit_op, var_form, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'))) result = core.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def test_excitation_preserving(self): """Test the excitation preserving wavefunction on a chemistry example.""" driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) wavefunction = ExcitationPreserving(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, wavefunction, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) result = operator.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def test_vqe_auto_symmetry_freeze_core(self): """ Auto symmetry reduction, with freeze core using VQE """ core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=True, orbital_reduction=None, z2symmetry_reduction='auto') qubit_op, aux_ops = core.run(self.qmolecule) self.assertEqual(qubit_op.num_qubits, 6) num_orbitals = core.molecule_info[core.INFO_NUM_ORBITALS] num_particles = core.molecule_info[core.INFO_NUM_PARTICLES] qubit_mapping = 'jordan_wigner' two_qubit_reduction = core.molecule_info[core.INFO_TWO_QUBIT_REDUCTION] z2_symmetries = core.molecule_info[core.INFO_Z2SYMMETRIES] initial_state = HartreeFock(num_orbitals, num_particles, qubit_mapping, two_qubit_reduction, z2_symmetries.sq_list) var_form = UCCSD(num_orbitals=num_orbitals, num_particles=num_particles, initial_state=initial_state, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction, z2_symmetries=z2_symmetries) vqe = VQE(qubit_op, var_form=var_form, optimizer=SLSQP(maxiter=500), aux_operators=aux_ops) vqe.quantum_instance = BasicAer.get_backend('statevector_simulator') result = core.process_algorithm_result(vqe.compute_minimum_eigenvalue()) self._validate_result(result) self.assertEqual(core.molecule_info[core.INFO_Z2SYMMETRIES].tapering_values, [-1, 1, 1, -1])
def setUp(self): super().setUp() self.reference_energy = -1.1373060356951838 self.seed = 700 aqua_globals.random_seed = self.seed driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() core = Hamiltonian(qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True) self.qubit_op, _ = core.run(qmolecule) self.core = core self.optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( core.molecule_info['num_orbitals'], core.molecule_info['num_particles'], qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) self.var_form = UCCSD( num_orbitals=core.molecule_info['num_orbitals'], num_particles=core.molecule_info['num_particles'], initial_state=initial_state, qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction)
def test_swaprz(self, mode): """ SwapRZ variational form test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) if mode == 'wrapped': warnings.filterwarnings('ignore', category=DeprecationWarning) wavefunction = SwapRZ(qubit_op.num_qubits, initial_state=initial_state) else: wavefunction = ExcitationPreserving(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, wavefunction, optimizer) if mode == 'wrapped': warnings.filterwarnings('always', category=DeprecationWarning) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) result = operator.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def test_h2_one_qubit(self): """Test H2 with tapering.""" two_qubit_reduction = False qubit_mapping = 'jordan_wigner' core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=two_qubit_reduction, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(self.molecule) num_orbitals = core.molecule_info['num_orbitals'] num_particles = core.molecule_info['num_particles'] z2_symmetries = Z2Symmetries.find_Z2_symmetries(qubit_op) tapered_op = z2_symmetries.taper(qubit_op)[5] eom_ee = QEomEE(tapered_op, num_orbitals=num_orbitals, num_particles=num_particles, qubit_mapping=qubit_mapping, two_qubit_reduction=two_qubit_reduction, z2_symmetries=tapered_op.z2_symmetries, untapered_op=qubit_op) result = eom_ee.run() np.testing.assert_array_almost_equal(self.reference, result['energies'])
def test_output(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, actual_two_qubit_reduction=True) self._validate_input_object(input_object, num_qubits=2, num_paulis=5)
def test_orbital_reduction(self): # Remove virtual orbital just for test purposes (not sensible!) core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[-1]) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, num_orbitals=2) self._validate_input_object(input_object, num_qubits=2, num_paulis=4)
def test_jordan_wigner(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(input_object)
def test_parity(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) qubit_op, aux_ops = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(qubit_op)
def test_freeze_core(self): # Should be in effect a no-op for H2 core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=True, orbital_reduction=[]) qubit_op, aux_ops = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(qubit_op)
def test_particle_hole(self): core = Hamiltonian(transformation=TransformationType.PH, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) qubit_op, aux_ops = core.run(self.qmolecule) self._validate_vars(core, ph_energy_shift=-1.83696799) self._validate_info(core) self._validate_input_object(qubit_op)
def test_bravyi_kitaev(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.BRAVYI_KITAEV, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(input_object)
def test_freeze_core_orb_reduction(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False, freeze_core=True, orbital_reduction=[-3, -2]) qubit_op, aux_ops = core.run(self.qmolecule) self._validate_vars(core, energy_shift=-7.7962196) self._validate_info(core, num_particles=2, num_orbitals=6) self._validate_input_object(qubit_op, num_qubits=6, num_paulis=118)
def test_given_symmetry_fail_len(self): """ Supplied symmetry reduction invalid len """ with self.assertRaises(QiskitChemistryError): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=None, z2symmetry_reduction=[1, 1, 1]) _, _ = core.run(self.qmolecule)
def test_jordan_wigner_2q(self): core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) qubit_op, aux_ops = core.run(self.qmolecule) self._validate_vars(core) # Reported effective 2 qubit reduction should be false self._validate_info(core, actual_two_qubit_reduction=False) self._validate_input_object(qubit_op)
def test_output(self): """ output test """ core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(qubit_op)
def test_parity(self): """ parity test """ core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, actual_two_qubit_reduction=True) self._validate_input_object(qubit_op, num_qubits=10)
def test_freeze_core(self): """ freeze core test """ core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False, freeze_core=True, orbital_reduction=[]) qubit_op, _ = core.run(self.qmolecule) self._validate_vars(core, energy_shift=-7.7962196) self._validate_info(core, num_particles=[1, 1], num_orbitals=10) self._validate_input_object(qubit_op, num_qubits=10, num_paulis=276)
def test_freeze_core_all_reduction_ph(self): """ freeze core all reduction ph test """ core = Hamiltonian(transformation=TransformationType.PH, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=True, orbital_reduction=[-2, -1]) qubit_op, _ = core.run(self.qmolecule) self._validate_vars(core, energy_shift=-7.7962196, ph_energy_shift=-1.05785247) self._validate_info(core, num_particles=[1, 1], num_orbitals=6, actual_two_qubit_reduction=True) self._validate_input_object(qubit_op, num_qubits=4, num_paulis=52)
def compute_energy( self, callback: Optional[Callable[[List, int, str, bool, Z2Symmetries], MinimumEigensolver]] = None ) -> MolecularGroundStateResult: """ Compute the ground state energy of the molecule that was supplied via the driver Args: callback: If not None will be called with the following values num_particles, num_orbitals, qubit_mapping, two_qubit_reduction, z2_symmetries in that order. This information can then be used to setup chemistry specific component(s) that are needed by the chosen MinimumEigensolver. The MinimumEigensolver can then be built and returned from this callback for use as the solver here. Returns: A molecular ground state result Raises: QiskitChemistryError: If no MinimumEigensolver was given and no callback is being used that could supply one instead. """ if self.solver is None and callback is None: raise QiskitChemistryError('MinimumEigensolver was not provided') q_molecule = self.driver.run() core = Hamiltonian(transformation=self._transformation, qubit_mapping=self._qubit_mapping, two_qubit_reduction=self._two_qubit_reduction, freeze_core=self._freeze_core, orbital_reduction=self._orbital_reduction, z2symmetry_reduction=self._z2symmetry_reduction) operator, aux_operators = core.run(q_molecule) if callback is not None: num_particles = core.molecule_info[ ChemistryOperator.INFO_NUM_PARTICLES] num_orbitals = core.molecule_info[ ChemistryOperator.INFO_NUM_ORBITALS] two_qubit_reduction = core.molecule_info[ ChemistryOperator.INFO_TWO_QUBIT_REDUCTION] z2_symmetries = core.molecule_info[ ChemistryOperator.INFO_Z2SYMMETRIES] self.solver = callback(num_particles, num_orbitals, self._qubit_mapping.value, two_qubit_reduction, z2_symmetries) aux_operators = aux_operators if self.solver.supports_aux_operators( ) else None raw_result = self.solver.compute_minimum_eigenvalue( operator, aux_operators) return core.process_algorithm_result(raw_result)
def test_given_symmetry_fail_values(self): """ Supplied symmetry reduction invalid values """ with self.assertRaises(QiskitChemistryError): warnings.filterwarnings('ignore', category=DeprecationWarning) core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=None, z2symmetry_reduction=[1, 0, 1, 1]) warnings.filterwarnings('always', category=DeprecationWarning) _, _ = core.run(self.qmolecule)
def test_no_symmetry(self): """ No symmetry reduction """ core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=False, orbital_reduction=None, z2symmetry_reduction=None) qubit_op, aux_ops = core.run(self.qmolecule) self.assertEqual(qubit_op.num_qubits, 12) npme = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops) result = core.process_algorithm_result(npme.compute_minimum_eigenvalue()) self._validate_result(result, False)
def test_parity(self): """ parity test """ warnings.filterwarnings('ignore', category=DeprecationWarning) core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) warnings.filterwarnings('always', category=DeprecationWarning) qubit_op, _ = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, actual_two_qubit_reduction=True) self._validate_input_object(qubit_op, num_qubits=10)