def test_aqgd(self):
        """Test AQGD is serializable."""

        opt = AQGD(maxiter=[200, 100], eta=[0.2, 0.1], momentum=[0.25, 0.1])
        settings = opt.settings

        self.assertListEqual(settings["maxiter"], [200, 100])
        self.assertListEqual(settings["eta"], [0.2, 0.1])
        self.assertListEqual(settings["momentum"], [0.25, 0.1])
Beispiel #2
0
    def test_int_values(self):
        """ test AQGD with int values passed as eta and momentum. """
        q_instance = QuantumInstance(
            BasicAer.get_backend('statevector_simulator'),
            seed_simulator=aqua_globals.random_seed,
            seed_transpiler=aqua_globals.random_seed)

        aqgd = AQGD(maxiter=1000, eta=1, momentum=0)
        result = VQE(self.qubit_op, RealAmplitudes(), aqgd).run(q_instance)
        self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
Beispiel #3
0
    def test_simple(self):
        """ test AQGD optimizer with the parameters as single values."""
        q_instance = QuantumInstance(
            BasicAer.get_backend('statevector_simulator'),
            seed_simulator=aqua_globals.random_seed,
            seed_transpiler=aqua_globals.random_seed)

        aqgd = AQGD(momentum=0.0)
        result = VQE(self.qubit_op, RealAmplitudes(), aqgd).run(q_instance)
        self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
    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)
Beispiel #5
0
    def test_list(self):
        """ test AQGD optimizer with the parameters as lists. """
        q_instance = QuantumInstance(
            BasicAer.get_backend('statevector_simulator'),
            seed_simulator=aqua_globals.random_seed,
            seed_transpiler=aqua_globals.random_seed)

        aqgd = AQGD(maxiter=[1000, 1000, 1000],
                    eta=[1.0, 0.5, 0.3],
                    momentum=[0.0, 0.5, 0.75])
        result = VQE(self.qubit_op, RealAmplitudes(), aqgd).run(q_instance)
        self.assertAlmostEqual(result.eigenvalue.real, -1.857, places=3)
    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)
Beispiel #8
0
    def test_h2_bopes_sampler(self):
        """Test BOPES Sampler on H2"""
        seed = 50
        algorithm_globals.random_seed = seed

        # Molecule
        dof = partial(Molecule.absolute_distance, atom_pair=(1, 0))
        m = Molecule(geometry=[['H', [0., 0., 1.]],
                               ['H', [0., 0.45, 1.]]],
                     degrees_of_freedom=[dof])

        f_t = FermionicTransformation()
        driver = PySCFDriver(molecule=m)

        qubitop, _ = f_t.transform(driver)

        # Quantum Instance:
        shots = 1
        backend = 'statevector_simulator'
        quantum_instance = QuantumInstance(BasicAer.get_backend(backend), shots=shots)
        quantum_instance.run_config.seed_simulator = seed
        quantum_instance.compile_config['seed_transpiler'] = seed

        # Variational form
        i_state = HartreeFock(num_orbitals=f_t._molecule_info['num_orbitals'],
                              qubit_mapping=f_t._qubit_mapping,
                              two_qubit_reduction=f_t._two_qubit_reduction,
                              num_particles=f_t._molecule_info['num_particles'],
                              sq_list=f_t._molecule_info['z2_symmetries'].sq_list
                              )
        var_form = RealAmplitudes(qubitop.num_qubits, reps=1, entanglement='full',
                                  skip_unentangled_qubits=False)
        var_form.compose(i_state, front=True)

        # Classical optimizer:
        # Analytic Quantum Gradient Descent (AQGD) (with Epochs)
        aqgd_max_iter = [10] + [1] * 100
        aqgd_eta = [1e0] + [1.0 / k for k in range(1, 101)]
        aqgd_momentum = [0.5] + [0.5] * 100
        optimizer = AQGD(maxiter=aqgd_max_iter,
                         eta=aqgd_eta,
                         momentum=aqgd_momentum,
                         tol=1e-6,
                         averaging=4)

        # Min Eigensolver: VQE
        solver = VQE(var_form=var_form,
                     optimizer=optimizer,
                     quantum_instance=quantum_instance,
                     expectation=PauliExpectation())

        me_gss = GroundStateEigensolver(f_t, solver)

        # BOPES sampler
        sampler = BOPESSampler(gss=me_gss)

        # absolute internuclear distance in Angstrom
        points = [0.7, 1.0, 1.3]
        results = sampler.sample(driver, points)

        points_run = results.points
        energies = results.energies

        np.testing.assert_array_almost_equal(points_run, [0.7, 1.0, 1.3])
        np.testing.assert_array_almost_equal(energies,
                                             [-1.13618945, -1.10115033, -1.03518627], decimal=2)