def test_backend_change(self, user_expectation): """Test that VQE works when backend changes.""" vqe = VQE( ansatz=TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz"), optimizer=SLSQP(maxiter=2), expectation=user_expectation, quantum_instance=BasicAer.get_backend("statevector_simulator"), ) result0 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest("User expectation kept."): self.assertEqual(vqe.expectation, user_expectation) vqe.quantum_instance = BasicAer.get_backend("qasm_simulator") # works also if no expectation is set, since it will be determined automatically result1 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest( "Change backend with user expectation, it is kept."): self.assertEqual(vqe.expectation, user_expectation) with self.subTest("Check results."): self.assertEqual(len(result0.optimal_point), len(result1.optimal_point))
def test_callback(self): """Test the callback on VQE.""" history = {"eval_count": [], "parameters": [], "mean": [], "std": []} def store_intermediate_result(eval_count, parameters, mean, std): history["eval_count"].append(eval_count) history["parameters"].append(parameters) history["mean"].append(mean) history["std"].append(std) optimizer = COBYLA(maxiter=3) wavefunction = self.ry_wavefunction vqe = VQE( ansatz=wavefunction, optimizer=optimizer, callback=store_intermediate_result, quantum_instance=self.qasm_simulator, ) vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertTrue( all(isinstance(count, int) for count in history["eval_count"])) self.assertTrue( all(isinstance(mean, float) for mean in history["mean"])) self.assertTrue(all(isinstance(std, float) for std in history["std"])) for params in history["parameters"]: self.assertTrue(all(isinstance(param, float) for param in params))
def test_with_two_qubit_reduction(self): """Test the VQE using TwoQubitReduction.""" qubit_op = PauliSumOp.from_list([ ("IIII", -0.8105479805373266), ("IIIZ", 0.17218393261915552), ("IIZZ", -0.22575349222402472), ("IZZI", 0.1721839326191556), ("ZZII", -0.22575349222402466), ("IIZI", 0.1209126326177663), ("IZZZ", 0.16892753870087912), ("IXZX", -0.045232799946057854), ("ZXIX", 0.045232799946057854), ("IXIX", 0.045232799946057854), ("ZXZX", -0.045232799946057854), ("ZZIZ", 0.16614543256382414), ("IZIZ", 0.16614543256382414), ("ZZZZ", 0.17464343068300453), ("ZIZI", 0.1209126326177663), ]) tapered_qubit_op = TwoQubitReduction(num_particles=2).convert(qubit_op) for simulator in [self.qasm_simulator, self.statevector_simulator]: with self.subTest(f"Test for {simulator}."): vqe = VQE( self.ry_wavefunction, SPSA(maxiter=300, last_avg=5), quantum_instance=simulator, ) result = vqe.compute_minimum_eigenvalue(tapered_qubit_op) energy = -1.868 if simulator == self.qasm_simulator else self.h2_energy self.assertAlmostEqual(result.eigenvalue.real, energy, places=2)
def test_qasm_aux_operators_normalized(self): """Test VQE with qasm_simulator returns normalized aux_operator eigenvalues.""" wavefunction = self.ry_wavefunction vqe = VQE(ansatz=wavefunction, quantum_instance=self.qasm_simulator) _ = vqe.compute_minimum_eigenvalue(operator=self.h2_op) opt_params = [ 3.50437328, 3.87415376, 0.93684363, 5.92219622, -1.53527887, 1.87941418, -4.5708326, 0.70187027, ] vqe._ret.optimal_point = opt_params vqe._ret.optimal_parameters = dict( zip(sorted(wavefunction.parameters, key=lambda p: p.name), opt_params)) with self.assertWarns(DeprecationWarning): optimal_vector = vqe.get_optimal_vector() self.assertAlmostEqual(sum(v**2 for v in optimal_vector.values()), 1.0, places=4)
def test_basic_aer_statevector(self): """Test the VQE on BasicAer's statevector simulator.""" wavefunction = self.ryrz_wavefunction vqe = VQE( ansatz=wavefunction, optimizer=L_BFGS_B(), quantum_instance=QuantumInstance( BasicAer.get_backend("statevector_simulator"), basis_gates=["u1", "u2", "u3", "cx", "id"], coupling_map=[[0, 1]], seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ), ) result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) with self.subTest(msg="test eigenvalue"): self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy) with self.subTest(msg="test dimension of optimal point"): self.assertEqual(len(result.optimal_point), 16) with self.subTest(msg="assert cost_function_evals is set"): self.assertIsNotNone(result.cost_function_evals) with self.subTest(msg="assert optimizer_time is set"): self.assertIsNotNone(result.optimizer_time)
def test_backend_change(self, user_expectation): """Test that VQE works when backend changes.""" vqe = VQE( ansatz=TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz"), optimizer=SLSQP(maxiter=2), expectation=user_expectation, quantum_instance=BasicAer.get_backend("statevector_simulator"), ) result0 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest("User expectation kept."): self.assertEqual(vqe.expectation, user_expectation) else: with self.subTest("Expectation created."): self.assertIsInstance(vqe.expectation, ExpectationBase) try: vqe.quantum_instance = BasicAer.get_backend("qasm_simulator") except Exception as ex: # pylint: disable=broad-except self.fail("Failed to change backend. Error: '{}'".format(str(ex))) return result1 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest( "Change backend with user expectation, it is kept."): self.assertEqual(vqe.expectation, user_expectation) else: with self.subTest( "Change backend without user expectation, one created."): self.assertIsInstance(vqe.expectation, ExpectationBase) with self.subTest("Check results."): self.assertEqual(len(result0.optimal_point), len(result1.optimal_point))
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_qasm_eigenvector_normalized(self): """Test VQE with qasm_simulator returns normalized eigenvector.""" wavefunction = self.ry_wavefunction vqe = VQE(ansatz=wavefunction, quantum_instance=self.qasm_simulator) result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) amplitudes = list(result.eigenstate.values()) self.assertAlmostEqual(np.linalg.norm(amplitudes), 1.0, places=4)
def test_missing_varform_params(self): """Test specifying a variational form with no parameters raises an error.""" circuit = QuantumCircuit(self.h2_op.num_qubits) vqe = VQE( ansatz=circuit, quantum_instance=BasicAer.get_backend("statevector_simulator")) with self.assertRaises(RuntimeError): vqe.compute_minimum_eigenvalue(operator=self.h2_op)
def test_aux_operator_std_dev_aer_pauli(self): """Test non-zero standard deviations of aux operators with AerPauliExpectation.""" wavefunction = self.ry_wavefunction vqe = VQE( ansatz=wavefunction, expectation=AerPauliExpectation(), optimizer=COBYLA(maxiter=0), quantum_instance=QuantumInstance( backend=Aer.get_backend("qasm_simulator"), shots=1, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ), ) # Go again with two auxiliary operators aux_op1 = PauliSumOp.from_list([("II", 2.0)]) aux_op2 = PauliSumOp.from_list([("II", 0.5), ("ZZ", 0.5), ("YY", 0.5), ("XX", -0.5)]) aux_ops = [aux_op1, aux_op2] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=aux_ops) self.assertEqual(len(result.aux_operator_eigenvalues), 2) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2.0, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0.6698863565455391, places=6) # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.0, places=6) # Go again with additional None and zero operators aux_ops = [*aux_ops, None, 0] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=aux_ops) self.assertEqual(len(result.aux_operator_eigenvalues), 4) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2.0, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0.6036400943063891, places=6) self.assertEqual(result.aux_operator_eigenvalues[2][0], 0.0) self.assertEqual(result.aux_operator_eigenvalues[3][0], 0.0) # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.0, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[2][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[3][1], 0.0)
def test_optimizer_callable(self): """Test passing a optimizer directly as callable.""" ansatz = RealAmplitudes(1, reps=1) vqe = VQE(ansatz=ansatz, optimizer=_mock_optimizer, quantum_instance=self.statevector_simulator) result = vqe.compute_minimum_eigenvalue(Z) self.assertTrue( np.all(result.optimal_point == np.zeros(ansatz.num_parameters)))
def test_aux_operators_list(self): """Test list-based aux_operators.""" wavefunction = self.ry_wavefunction vqe = VQE(ansatz=wavefunction, quantum_instance=self.statevector_simulator) # Start with an empty list result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=[]) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6) self.assertIsNone(result.aux_operator_eigenvalues) # Go again with two auxiliary operators aux_op1 = PauliSumOp.from_list([("II", 2.0)]) aux_op2 = PauliSumOp.from_list([("II", 0.5), ("ZZ", 0.5), ("YY", 0.5), ("XX", -0.5)]) aux_ops = [aux_op1, aux_op2] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=aux_ops) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6) self.assertEqual(len(result.aux_operator_eigenvalues), 2) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0, places=6) # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.0) # Go again with additional None and zero operators extra_ops = [*aux_ops, None, 0] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=extra_ops) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6) self.assertEqual(len(result.aux_operator_eigenvalues), 4) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0, places=6) self.assertEqual(result.aux_operator_eigenvalues[2][0], 0.0) self.assertEqual(result.aux_operator_eigenvalues[3][0], 0.0) # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[2][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[3][1], 0.0)
def test_set_optimizer_to_none(self): """Tests that setting the optimizer to None results in the default behavior""" vqe = VQE( ansatz=self.ryrz_wavefunction, optimizer=L_BFGS_B(), quantum_instance=self.statevector_simulator, ) vqe.optimizer = None self.assertIsInstance(vqe.optimizer, SLSQP)
def test_circuit_input(self): """Test running the VQE on a plain QuantumCircuit object.""" wavefunction = QuantumCircuit(2).compose(EfficientSU2(2)) optimizer = SLSQP(maxiter=50) vqe = VQE(self.h2_op, wavefunction, optimizer=optimizer) result = vqe.run(self.statevector_simulator) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5)
def test_optimizer_scipy_callable(self): """Test passing a SciPy optimizer directly as callable.""" vqe = VQE( optimizer=partial(scipy_minimize, method="L-BFGS-B", options={"maxiter": 2}), quantum_instance=self.statevector_simulator, ) result = vqe.compute_minimum_eigenvalue(Z) self.assertEqual(result.cost_function_evals, 20)
def test_end2end_h2(self): """ end to end h2 """ backend = BasicAer.get_backend('statevector_simulator') shots = 1 optimizer = COBYLA(maxiter=1000) ryrz = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz') quantum_instance = QuantumInstance(backend, shots=shots) vqe = VQE(ryrz, optimizer=optimizer, quantum_instance=quantum_instance) result = vqe.compute_minimum_eigenvalue(self.qubit_op, aux_operators=self.aux_ops) self.assertAlmostEqual(result.eigenvalue.real, self.reference_energy, places=4)
def _optimize(self, optimizer): """ launch vqe """ qe = QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed) vqe = VQE(var_form=RealAmplitudes(), optimizer=optimizer, quantum_instance=qe) result = vqe.compute_minimum_eigenvalue(operator=self.qubit_op) self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=1)
def test_basic_aer_qasm(self): """Test the VQE on BasicAer's QASM simulator.""" optimizer = SPSA(maxiter=300, last_avg=5) wavefunction = self.ry_wavefunction vqe = VQE(self.h2_op, wavefunction, optimizer, max_evals_grouped=1) # TODO benchmark this later. result = vqe.run(self.qasm_simulator) self.assertAlmostEqual(result.eigenvalue.real, -1.86823, places=2)
def test_max_evals_grouped(self, optimizer, places, max_evals_grouped): """ VQE Optimizers test """ vqe = VQE(var_form=self.ryrz_wavefunction, optimizer=optimizer, max_evals_grouped=max_evals_grouped, quantum_instance=self.statevector_simulator) result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=places)
def test_reuse(self): """Test re-using a VQE algorithm instance.""" vqe = VQE() with self.subTest(msg="assert running empty raises AlgorithmError"): with self.assertRaises(AlgorithmError): _ = vqe.compute_minimum_eigenvalue(operator=self.h2_op) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") vqe.ansatz = ansatz with self.subTest(msg="assert missing operator raises AlgorithmError"): with self.assertRaises(AlgorithmError): _ = vqe.compute_minimum_eigenvalue(operator=self.h2_op) vqe.expectation = MatrixExpectation() vqe.quantum_instance = self.statevector_simulator with self.subTest(msg="assert VQE works once all info is available"): result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5) operator = PrimitiveOp( np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]])) with self.subTest(msg="assert minimum eigensolver interface works"): result = vqe.compute_minimum_eigenvalue(operator=operator) self.assertAlmostEqual(result.eigenvalue.real, -1.0, places=5)
def test_nft(self): """ Test NFT optimizer by using it """ vqe = VQE(var_form=RealAmplitudes(), optimizer=NFT(), quantum_instance=QuantumInstance( BasicAer.get_backend('statevector_simulator'), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed)) result = vqe.compute_minimum_eigenvalue(operator=self.qubit_op) self.assertAlmostEqual(result.eigenvalue.real, -1.857275, places=6)
def test_list(self): """ test AQGD optimizer with the parameters as lists. """ q_instance = QuantumInstance(Aer.get_backend('statevector_simulator'), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed) aqgd = AQGD(maxiter=[1000, 1000, 1000], eta=[1.0, 0.5, 0.3], momentum=[0.0, 0.5, 0.75]) vqe = VQE(ansatz=RealAmplitudes(), optimizer=aqgd, quantum_instance=q_instance) result = vqe.compute_minimum_eigenvalue(operator=self.qubit_op) self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
def test_max_evals_grouped(self, optimizer, places, max_evals_grouped): """VQE Optimizers test""" with self.assertWarns(DeprecationWarning): vqe = VQE( ansatz=self.ryrz_wavefunction, optimizer=optimizer, max_evals_grouped=max_evals_grouped, quantum_instance=self.statevector_simulator, sort_parameters_by_name=True, ) result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=places)
def test_aux_operator_std_dev_pauli(self): """Test non-zero standard deviations of aux operators with PauliExpectation.""" wavefunction = self.ry_wavefunction vqe = VQE( ansatz=wavefunction, expectation=PauliExpectation(), optimizer=COBYLA(maxiter=0), quantum_instance=self.qasm_simulator, ) # Go again with two auxiliary operators aux_op1 = PauliSumOp.from_list([("II", 2.0)]) aux_op2 = PauliSumOp.from_list([("II", 0.5), ("ZZ", 0.5), ("YY", 0.5), ("XX", -0.5)]) aux_ops = [aux_op1, aux_op2] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=aux_ops) self.assertEqual(len(result.aux_operator_eigenvalues), 2) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2.0, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0.6796875, places=6) # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.02534712219145965, places=6) # Go again with additional None and zero operators aux_ops = [*aux_ops, None, 0] result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=aux_ops) self.assertEqual(len(result.aux_operator_eigenvalues), 4) # expectation values self.assertAlmostEqual(result.aux_operator_eigenvalues[0][0], 2.0, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][0], 0.57421875, places=6) self.assertEqual(result.aux_operator_eigenvalues[2][0], 0.0) self.assertEqual(result.aux_operator_eigenvalues[3][0], 0.0) # # standard deviations self.assertAlmostEqual(result.aux_operator_eigenvalues[0][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[1][1], 0.026562146577166837, places=6) self.assertAlmostEqual(result.aux_operator_eigenvalues[2][1], 0.0) self.assertAlmostEqual(result.aux_operator_eigenvalues[3][1], 0.0)
def test_basic_aer_qasm(self): """Test the VQE on BasicAer's QASM simulator.""" optimizer = SPSA(maxiter=300, last_avg=5) wavefunction = self.ry_wavefunction vqe = VQE(var_form=wavefunction, optimizer=optimizer, max_evals_grouped=1, quantum_instance=self.qasm_simulator) # TODO benchmark this later. result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertAlmostEqual(result.eigenvalue.real, -1.86823, places=2)
def test_int_values(self): """ test AQGD with int values passed as eta and momentum. """ q_instance = QuantumInstance(Aer.get_backend('statevector_simulator'), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed) aqgd = AQGD(maxiter=1000, eta=1, momentum=0) vqe = VQE(ansatz=RealAmplitudes(), optimizer=aqgd, gradient=Gradient('lin_comb'), quantum_instance=q_instance) result = vqe.compute_minimum_eigenvalue(operator=self.qubit_op) self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
def test_simple(self): """ test AQGD optimizer with the parameters as single values.""" q_instance = QuantumInstance(Aer.get_backend('statevector_simulator'), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed) aqgd = AQGD(momentum=0.0) vqe = VQE(ansatz=RealAmplitudes(), optimizer=aqgd, gradient=Gradient('fin_diff'), quantum_instance=q_instance) result = vqe.compute_minimum_eigenvalue(operator=self.qubit_op) self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
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 test_construct_circuit(self, expectation, num_circuits): """Test construct circuits returns QuantumCircuits and the right number of them.""" try: wavefunction = EfficientSU2(2, reps=1) vqe = VQE(self.h2_op, wavefunction, expectation=expectation) params = [0] * wavefunction.num_parameters circuits = vqe.construct_circuit(params) self.assertEqual(len(circuits), num_circuits) for circuit in circuits: self.assertIsInstance(circuit, QuantumCircuit) except MissingOptionalLibraryError as ex: self.skipTest(str(ex)) return
def test_construct_eigenstate_from_optpoint(self): """Test constructing the eigenstate from the optimal point, if the default ansatz is used.""" # use Hamiltonian yielding more than 11 parameters in the default ansatz hamiltonian = Z ^ Z ^ Z optimizer = SPSA(maxiter=1, learning_rate=0.01, perturbation=0.01) quantum_instance = QuantumInstance( backend=BasicAer.get_backend("statevector_simulator"), basis_gates=["u3", "cx"]) vqe = VQE(optimizer=optimizer, quantum_instance=quantum_instance) result = vqe.compute_minimum_eigenvalue(hamiltonian) optimal_circuit = vqe.ansatz.bind_parameters(result.optimal_point) self.assertTrue(Statevector(result.eigenstate).equiv(optimal_circuit))