def test_chc_vscf(self): """ chc vscf test """ co2_2modes_2modals_2body = [ [[[[0, 0, 0]], 320.8467332810141], [[[0, 1, 1]], 1760.878530705873], [[[1, 0, 0]], 342.8218290247543], [[[1, 1, 1]], 1032.396323618631]], [[[[0, 0, 0], [1, 0, 0]], -57.34003649795117], [[[0, 0, 1], [1, 0, 0]], -56.33205925807966], [[[0, 1, 0], [1, 0, 0]], -56.33205925807966], [[[0, 1, 1], [1, 0, 0]], -60.13032761856809], [[[0, 0, 0], [1, 0, 1]], -65.09576309934431], [[[0, 0, 1], [1, 0, 1]], -62.2363839133389], [[[0, 1, 0], [1, 0, 1]], -62.2363839133389], [[[0, 1, 1], [1, 0, 1]], -121.5533969109279], [[[0, 0, 0], [1, 1, 0]], -65.09576309934431], [[[0, 0, 1], [1, 1, 0]], -62.2363839133389], [[[0, 1, 0], [1, 1, 0]], -62.2363839133389], [[[0, 1, 1], [1, 1, 0]], -121.5533969109279], [[[0, 0, 0], [1, 1, 1]], -170.744837386338], [[[0, 0, 1], [1, 1, 1]], -167.7433236025723], [[[0, 1, 0], [1, 1, 1]], -167.7433236025723], [[[0, 1, 1], [1, 1, 1]], -179.0536532281924]] ] num_modes = 2 num_modals = [2, 2] vibrational_op_labels = _create_labels(co2_2modes_2modals_2body) vibr_op = VibrationalOp(vibrational_op_labels, num_modes, num_modals) converter = QubitConverter(DirectMapper()) qubit_op = converter.convert_match(vibr_op) init_state = VSCF(num_modals) num_qubits = sum(num_modals) excitations = [] excitations += generate_vibration_excitations(num_excitations=1, num_modals=num_modals) excitations += generate_vibration_excitations(num_excitations=2, num_modals=num_modals) chc_ansatz = CHC(num_qubits, ladder=False, excitations=excitations, initial_state=init_state) backend = QuantumInstance( BasicAer.get_backend('statevector_simulator'), seed_transpiler=2, seed_simulator=2) optimizer = COBYLA(maxiter=1000) algo = VQE(chc_ansatz, optimizer=optimizer, quantum_instance=backend) vqe_result = algo.compute_minimum_eigenvalue(qubit_op) energy = vqe_result.optimal_value self.assertAlmostEqual(energy, self.reference_energy, places=4)
def test_qubits_5(self): """Test 2 modes 2 modals for the first mode and 3 modals for the second.""" basis = [2, 3] vscf = VSCF(basis) ref = QuantumCircuit(5) ref.x([0, 2]) self.assertEqual(ref, vscf)
def test_qubits_4(self): """Test 2 modes 2 modals.""" basis = [2, 2] vscf = VSCF(basis) ref = QuantumCircuit(4) ref.x([0, 2]) self.assertEqual(ref, vscf)
def test_chc_vscf(self): """ chc vscf test """ co2_2modes_2modals_2body = [[[[[0, 0, 0]], 320.8467332810141], [[[0, 1, 1]], 1760.878530705873], [[[1, 0, 0]], 342.8218290247543], [[[1, 1, 1]], 1032.396323618631]], [[[[0, 0, 0], [1, 0, 0]], -57.34003649795117], [[[0, 0, 1], [1, 0, 0]], -56.33205925807966], [[[0, 1, 0], [1, 0, 0]], -56.33205925807966], [[[0, 1, 1], [1, 0, 0]], -60.13032761856809], [[[0, 0, 0], [1, 0, 1]], -65.09576309934431], [[[0, 0, 1], [1, 0, 1]], -62.2363839133389], [[[0, 1, 0], [1, 0, 1]], -62.2363839133389], [[[0, 1, 1], [1, 0, 1]], -121.5533969109279], [[[0, 0, 0], [1, 1, 0]], -65.09576309934431], [[[0, 0, 1], [1, 1, 0]], -62.2363839133389], [[[0, 1, 0], [1, 1, 0]], -62.2363839133389], [[[0, 1, 1], [1, 1, 0]], -121.5533969109279], [[[0, 0, 0], [1, 1, 1]], -170.744837386338], [[[0, 0, 1], [1, 1, 1]], -167.7433236025723], [[[0, 1, 0], [1, 1, 1]], -167.7433236025723], [[[0, 1, 1], [1, 1, 1]], -179.0536532281924]]] basis = [2, 2] bosonic_op = BosonicOperator(co2_2modes_2modals_2body, basis) qubit_op = bosonic_op.mapping('direct', threshold=1e-5) with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) init_state = VSCF(basis) num_qubits = sum(basis) uvcc_varform = UVCC(num_qubits, basis, [0, 1]) excitations = uvcc_varform.excitations_in_qubit_format() chc_varform = CHC(num_qubits, ladder=False, excitations=excitations, initial_state=init_state) backend = QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_transpiler=2, seed_simulator=2) optimizer = COBYLA(maxiter=1000) algo = VQE(chc_varform, optimizer=optimizer, quantum_instance=backend) vqe_result = algo.compute_minimum_eigenvalue(qubit_op) energy = vqe_result['optimal_value'] self.assertAlmostEqual(energy, self.reference_energy, places=4)
def get_solver( # type: ignore[override] self, problem: VibrationalStructureProblem, qubit_converter: QubitConverter, ) -> MinimumEigensolver: """Returns a VQE with a UVCCSD wavefunction ansatz, based on ``qubit_converter``. Args: problem: a class encoding a problem to be solved. qubit_converter: a class that converts second quantized operator to qubit operator according to a mapper it is initialized with. Returns: A VQE suitable to compute the ground state of the molecule. """ basis = cast(VibrationalStructureDriverResult, problem.grouped_property_transformed).basis num_modals = basis.num_modals_per_mode num_modes = len(num_modals) if isinstance(num_modals, int): num_modals = [num_modals] * num_modes initial_state = self.initial_state if initial_state is None: initial_state = VSCF(num_modals) ansatz = self.ansatz if ansatz is None: ansatz = UVCCSD() ansatz.qubit_converter = qubit_converter ansatz.num_modals = num_modals ansatz.initial_state = initial_state # TODO: leverage re-usability of VQE after fixing # https://github.com/Qiskit/qiskit-terra/issues/7093 vqe = VQE( ansatz=ansatz, quantum_instance=self.quantum_instance, optimizer=self.optimizer, initial_point=self.initial_point, gradient=self.gradient, expectation=self.expectation, include_custom=self.include_custom, callback=self.callback, **self._kwargs, ) return vqe
def get_solver( # type: ignore[override] self, problem: VibrationalStructureProblem, qubit_converter: QubitConverter, ) -> MinimumEigensolver: """Returns a VQE with a :class:`~.UVCCSD` wavefunction ansatz, based on ``qubit_converter``. Args: problem: a class encoding a problem to be solved. qubit_converter: a class that converts second quantized operator to qubit operator according to a mapper it is initialized with. Returns: A VQE suitable to compute the ground state of the molecule. """ basis = cast(VibrationalStructureDriverResult, problem.grouped_property_transformed).basis num_modals = basis.num_modals_per_mode num_modes = len(num_modals) if isinstance(num_modals, int): num_modals = [num_modals] * num_modes initial_state = self.initial_state if initial_state is None: initial_state = VSCF(num_modals) ansatz = self._ansatz if ansatz is None: ansatz = UVCCSD() ansatz.qubit_converter = qubit_converter ansatz.num_modals = num_modals ansatz.initial_state = initial_state if isinstance(self.initial_point, InitialPoint): self.initial_point.ansatz = ansatz initial_point = self.initial_point.to_numpy_array() else: initial_point = self.initial_point self.minimum_eigensolver.initial_point = initial_point self.minimum_eigensolver.ansatz = ansatz return self.minimum_eigensolver
def get_solver( self, problem: VibrationalStructureProblem, # type: ignore[override] qubit_converter: QubitConverter ) -> MinimumEigensolver: """Returns a VQE with a UVCCSD wavefunction ansatz, based on ``transformation``. This works only with a ``BosonicTransformation``. Args: problem: a class encoding a problem to be solved. qubit_converter: a class that converts second quantized operator to qubit operator according to a mapper it is initialized with. Returns: A VQE suitable to compute the ground state of the molecule transformed by ``transformation``. """ watson_hamiltonian_transformed = cast( WatsonHamiltonian, problem.molecule_data_transformed) num_modals = problem.num_modals num_modes = watson_hamiltonian_transformed.num_modes if isinstance(num_modals, int): num_modals = [num_modals] * num_modes initial_state = self.initial_state if initial_state is None: initial_state = VSCF(num_modals) ansatz = self.ansatz if ansatz is None: ansatz = UVCCSD() ansatz.qubit_converter = qubit_converter ansatz.num_modals = num_modals ansatz.initial_state = initial_state self._vqe.ansatz = ansatz return self._vqe