def test_larger_tutorial_qubit_op(self): """Test the 3-modal qubit operator generated in the vibrational structure tutorial.""" num_modes = self.driver_result.num_modes num_modals = [3] * num_modes vibration_energy = self.driver_result.get_property("VibrationalEnergy") vibration_energy.basis = HarmonicBasis(num_modals) vibration_op = vibration_energy.second_q_ops()[0] mapper = DirectMapper() qubit_op = mapper.map(vibration_op) self.assertEqual(qubit_op, _num_modals_3_q_op)
def test_mapping(self): """Test mapping to qubit operator""" num_modes = self.driver_result.num_modes num_modals = [2] * num_modes vibration_energy = self.driver_result.get_property("VibrationalEnergy") vibration_energy.basis = HarmonicBasis(num_modals) vibration_op = vibration_energy.second_q_ops()[0] mapper = DirectMapper() qubit_op = mapper.map(vibration_op) self.assertEqual(qubit_op, _num_modals_2_q_op)
def setUp(self): super().setUp() algorithm_globals.random_seed = 8 self.driver = _DummyBosonicDriver() self.qubit_converter = QubitConverter(DirectMapper()) self.basis_size = 2 self.truncation_order = 2 self.vibrational_problem = VibrationalStructureProblem( self.driver, self.basis_size, self.truncation_order) self.qubit_converter = QubitConverter(DirectMapper()) self.vibrational_problem.second_q_ops() self.watson_hamiltonian = self.vibrational_problem.molecule_data self.num_modals = [self.basis_size] * self.watson_hamiltonian.num_modes
def test_ucc_ansatz(self, excitations, num_modals, expect): """Tests the UVCC Ansatz.""" converter = QubitConverter(DirectMapper()) ansatz = UVCC(qubit_converter=converter, num_modals=num_modals, excitations=excitations) assert_ucc_like_ansatz(self, ansatz, num_modals, expect)
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_build_uvcc(self): """Test building UVCC""" uvcc = UVCC() with self.subTest("Check defaulted construction"): self.assertIsNone(uvcc.num_modals) self.assertIsNone(uvcc.excitations) self.assertIsNone(uvcc.qubit_converter) self.assertIsNone(uvcc.operators) self.assertIsNone(uvcc.excitation_list) self.assertEqual(uvcc.num_qubits, 0) with self.assertRaises(ValueError): _ = uvcc.data with self.subTest("Set num modals"): uvcc.num_modals = [2, 2] self.assertListEqual(uvcc.num_modals, [2, 2]) self.assertIsNone(uvcc.operators) with self.assertRaises(ValueError): _ = uvcc.data with self.subTest("Set excitations"): uvcc.excitations = "sd" self.assertEqual(uvcc.excitations, "sd") self.assertIsNone(uvcc.operators) with self.assertRaises(ValueError): _ = uvcc.data with self.subTest("Set qubit converter to complete build"): converter = QubitConverter(DirectMapper()) uvcc.qubit_converter = converter self.assertEqual(uvcc.qubit_converter, converter) self.assertIsNotNone(uvcc.operators) self.assertEqual(len(uvcc.operators), 3) self.assertEqual(uvcc.num_qubits, 4) self.assertIsNotNone(uvcc.data) with self.subTest("Set custom operators"): self.assertEqual(len(uvcc.operators), 3) uvcc.operators = uvcc.operators[:2] self.assertEqual(len(uvcc.operators), 2) self.assertEqual(uvcc.num_qubits, 4) with self.subTest("Reset operators back to as per UVCC"): uvcc.operators = None self.assertEqual(uvcc.num_qubits, 4) self.assertIsNotNone(uvcc.operators) self.assertEqual(len(uvcc.operators), 3) with self.subTest("Set num modals differently"): uvcc.num_modals = [3, 3] self.assertEqual(uvcc.num_modals, [3, 3]) self.assertIsNotNone(uvcc.operators) self.assertEqual(len(uvcc.operators), 8) with self.subTest("Change excitations"): uvcc.excitations = "s" self.assertIsNotNone(uvcc.operators) self.assertEqual(len(uvcc.operators), 4)
def test_transpile_no_parameters(self): """Test transpilation without parameters""" qubit_converter = QubitConverter(mapper=DirectMapper()) ansatz = UVCC(qubit_converter=qubit_converter, num_modals=[2], excitations="s") ansatz = transpile(ansatz, optimization_level=3) self.assertEqual(ansatz.num_qubits, 2)
def test_mapping(self): """ Test mapping to qubit operator """ driver = GaussianForcesDriver(logfile=self.get_resource_path( 'CO2_freq_B3LYP_ccpVDZ.log', 'problems/second_quantization/vibrational/resources')) watson_hamiltonian = driver.run() num_modals = 2 truncation = 3 vibration_op = _build_vibrational_op(watson_hamiltonian, num_modals, truncation) mapper = DirectMapper() qubit_op = mapper.map(vibration_op) self.assertEqual(qubit_op, REFERENCE)
def test_mapping(self): """Test mapping to qubit operator""" with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) driver = GaussianForcesDriver(logfile=self.get_resource_path( "CO2_freq_B3LYP_ccpVDZ.log", "problems/second_quantization/vibrational/resources", )) driver_result = driver.run() num_modes = driver_result.num_modes num_modals = [2] * num_modes vibration_energy = driver_result.get_property("VibrationalEnergy") vibration_energy.basis = HarmonicBasis(num_modals) vibration_op = vibration_energy.second_q_ops()[0] mapper = DirectMapper() qubit_op = mapper.map(vibration_op) self.assertEqual(qubit_op, REFERENCE)
def setUp(self): super().setUp() algorithm_globals.random_seed = 8 self.reference_energies = [1889.95738428, 3294.21806197, 4287.26821341, 5819.76975784] self.driver = _DummyBosonicDriver() self.qubit_converter = QubitConverter(DirectMapper()) self.basis_size = 2 self.truncation_order = 2 self.vibrational_problem = VibrationalStructureProblem( self.driver, self.basis_size, self.truncation_order )
def __init__( self, num_modals: list[int], qubit_converter: QubitConverter | None = None, ) -> None: """ Args: num_modals: Is a list defining the number of modals per mode. E.g. for a 3 modes system with 4 modals per mode num_modals = [4,4,4] qubit_converter: a QubitConverter instance. This argument is currently being ignored because only a single use-case is supported at the time of release: that of the :class:`DirectMapper`. However, for future-compatibility of this functions signature, the argument has already been inserted. """ # get the bitstring encoding initial state bitstr = vscf_bitstring(num_modals) # encode the bitstring in a `VibrationalOp` label = ["+" if bit else "I" for bit in bitstr] bitstr_op = VibrationalOp("".join(label), num_modes=len(num_modals), num_modals=num_modals) # map the `VibrationalOp` to a qubit operator if qubit_converter is not None: logger.warning( "The only supported `QubitConverter` is one with a `DirectMapper` as the mapper " "instance. However you specified %s as an input, which will be ignored until more " "variants will be supported.", str(qubit_converter), ) qubit_converter = QubitConverter(DirectMapper()) qubit_op: PauliSumOp = qubit_converter.convert_match(bitstr_op) # construct the circuit qr = QuantumRegister(qubit_op.num_qubits, "q") super().__init__(qr, name="VSCF") # add gates in the right positions for i, bit in enumerate(qubit_op.primitive.paulis.x[0]): if bit: self.x(i)
def test_allows_two_qubit_reduction(self): """Test this returns False for this mapper""" mapper = DirectMapper() self.assertFalse(mapper.allows_two_qubit_reduction)