class TestInitialStateHartreeFock(QiskitAquaChemistryTestCase): def test_qubits_4_jw_h2(self): self.hf = HartreeFock(4, 4, 2, 'jordan_wigner', False) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) def test_qubits_4_py_h2(self): self.hf = HartreeFock(4, 4, 2, 'parity', False) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) def test_qubits_4_bk_h2(self): self.hf = HartreeFock(4, 4, 2, 'bravyi_kitaev', False) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) def test_qubits_2_py_h2(self): self.hf = HartreeFock(2, 4, 2, 'parity', True) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.0, 1.0, 0.0, 0.0]) def test_qubits_2_py_h2_cct(self): self.hf = HartreeFock(2, 4, 2, 'parity', True) cct = self.hf.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n' 'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n')
def test_qubits_6_py_lih_cct(self): self.hf = HartreeFock(6, 10, 2, 'parity', True, [1, 2]) cct = self.hf.construct_circuit('circuit') self.assertEqual( cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[6];\n' 'u3(3.14159265358979,0.0,3.14159265358979) q[0];\n' 'u3(3.14159265358979,0.0,3.14159265358979) q[1];\n')
def test_qubits_4_bk_h2(self): self.hf = HartreeFock(4, 4, 2, 'bravyi_kitaev', False) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ])
def test_qubits_4_jw_h2(self): self.hf = HartreeFock(4, 4, 2, 'jordan_wigner', False) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [ 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ])
def qc_solver(h_qop, num_spin_orbitals, num_particles, map_type, \ qubit_reduction, aux_qops=None): # backends = Aer.backends() backends = IBMQ.backends(simulator=False) print(backends) # backend = IBMQ.get_backend('ibmq_qasm_simulator') # backend = IBMQ.get_backend('ibmq_16_melbourne') # backend = Aer.get_backend('statevector_simulator') backend = Aer.get_backend('qasm_simulator') # setup COBYLA optimizer max_eval = 1000 cobyla = COBYLA(maxiter=max_eval) # setup hartreeFock state hf_state = HartreeFock(h_qop.num_qubits, num_spin_orbitals, num_particles, map_type, qubit_reduction) # setup UCCSD variational form var_form = UCCSD(h_qop.num_qubits, depth=1, num_orbitals=num_spin_orbitals, num_particles=num_particles, active_occupied=[0], active_unoccupied=[0], initial_state=hf_state, qubit_mapping=map_type, two_qubit_reduction=qubit_reduction, num_time_slices=1) # setup VQE vqe = VQE(h_qop, var_form, cobyla, operator_mode='matrix', \ aux_operators=aux_qops) quantum_instance = QuantumInstance(backend=backend, shots=1024, max_credits=10) ret = vqe.run(quantum_instance) print(ret['aux_ops']) print('The computed ground state energy is: {:.12f}'.format(\ ret['eigvals'][0]))
def test_iqpe(self, distance): self.algorithm = 'IQPE' self.log.debug('Testing End-to-End with IQPE on H2 with ' 'inter-atomic distance {}.'.format(distance)) try: driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 {}'.format(distance), unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.molecule = driver.run() qubit_mapping = 'parity' fer_op = FermionicOperator(h1=self.molecule.one_body_integrals, h2=self.molecule.two_body_integrals) self.qubit_op = fer_op.mapping(map_type=qubit_mapping, threshold=1e-10).two_qubit_reduced_operator(2) exact_eigensolver = ExactEigensolver(self.qubit_op, k=1) results = exact_eigensolver.run() self.reference_energy = results['energy'] self.log.debug('The exact ground state energy is: {}'.format(results['energy'])) num_particles = self.molecule.num_alpha + self.molecule.num_beta two_qubit_reduction = True num_orbitals = self.qubit_op.num_qubits + (2 if two_qubit_reduction else 0) num_time_slices = 50 num_iterations = 12 state_in = HartreeFock(self.qubit_op.num_qubits, num_orbitals, num_particles, qubit_mapping, two_qubit_reduction) iqpe = IQPE(self.qubit_op, state_in, num_time_slices, num_iterations, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) backend = qiskit.Aer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) result = iqpe.run(quantum_instance) self.log.debug('top result str label: {}'.format(result['top_measurement_label'])) self.log.debug('top result in decimal: {}'.format(result['top_measurement_decimal'])) self.log.debug('stretch: {}'.format(result['stretch'])) self.log.debug('translation: {}'.format(result['translation'])) self.log.debug('final energy from QPE: {}'.format(result['energy'])) self.log.debug('reference energy: {}'.format(self.reference_energy)) self.log.debug('ref energy (transformed): {}'.format( (self.reference_energy + result['translation']) * result['stretch']) ) self.log.debug('ref binary str label: {}'.format(decimal_to_binary( (self.reference_energy + result['translation']) * result['stretch'], max_num_digits=num_iterations + 3, fractional_part_only=True ))) np.testing.assert_approx_equal(result['energy'], self.reference_energy, significant=2)
ret = exact_eigensolver.run() print('The computed energy is: {:.12f}'.format(ret['eigvals'][0].real)) print('The total ground state energy is: {:.12f}'.format( ret['eigvals'][0].real + energy_shift + nuclear_repulsion_energy)) from qiskit import IBMQ IBMQ.load_accounts() backend = Aer.get_backend('statevector_simulator') # setup COBYLA optimizer max_eval = 200 cobyla = COBYLA(maxiter=max_eval) # setup HartreeFock state HF_state = HartreeFock(qubitOp.num_qubits, num_spin_orbitals, num_particles, map_type, qubit_reduction) # setup UCCSD variational form var_form = UCCSD(qubitOp.num_qubits, depth=1, num_orbitals=num_spin_orbitals, num_particles=num_particles, active_occupied=[0], active_unoccupied=[0, 1], initial_state=HF_state, qubit_mapping=map_type, two_qubit_reduction=qubit_reduction, num_time_slices=1) # setup VQE vqe = VQE(qubitOp, var_form, cobyla, 'matrix')
def test_qubits_2_py_h2(self): self.hf = HartreeFock(2, 4, 2, 'parity', True) cct = self.hf.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.0, 1.0, 0.0, 0.0])
for basis in bases: for qm_name, qubit_mapping in qubit_mappings.items(): for var_form_name, var_form_class in var_forms.items(): for mol_name, molecule_str in molecules.items(): driver = PySCFDriver(molecule_str, basis=basis) mol = driver.run() n_qubits = mol.one_body_integrals.shape[0] n_electrons = mol.num_alpha + mol.num_beta - mol.molecular_charge ferOp = FermionicOperator(h1=mol.one_body_integrals, h2=mol.two_body_integrals) qubitOp = ferOp.mapping(map_type=qubit_mapping, threshold=1e-8) qubitOp.chop(1e-10) initial_hf = HartreeFock(num_qubits=n_qubits, num_orbitals=n_qubits, qubit_mapping=qubit_mapping, two_qubit_reduction=False, num_particles=n_electrons) var_form = UCCSD(num_qubits=n_qubits, num_orbitals=n_qubits, num_particles=n_electrons, depth=1, initial_state=initial_hf, qubit_mapping=qubit_mapping) number_amplitudes = len(var_form._single_excitations) + len( var_form._double_excitations) amplitudes = [1e-4] * number_amplitudes circuit = var_form.construct_circuit(amplitudes) qasm = circuit.qasm() filename = '{m}_{v}_{q}_{b}.qasm'.format(m=mol_name, v=var_form_name,
def test_qpe(self, distance): self.algorithm = 'QPE' self.log.debug( 'Testing End-to-End with QPE on H2 with inter-atomic distance {}.'. format(distance)) cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'H .0 .0 .0; H .0 .0 {}'.format(distance)), ('unit', 'Angstrom'), ('charge', 0), ('spin', 0), ('basis', 'sto3g')]) section = {} section['properties'] = pyscf_cfg try: driver = cfg_mgr.get_driver_instance('PYSCF') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.molecule = driver.run(section) qubit_mapping = 'parity' fer_op = FermionicOperator(h1=self.molecule.one_body_integrals, h2=self.molecule.two_body_integrals) self.qubit_op = fer_op.mapping( map_type=qubit_mapping, threshold=1e-10).two_qubit_reduced_operator(2) exact_eigensolver = ExactEigensolver(self.qubit_op, k=1) results = exact_eigensolver.run() self.reference_energy = results['energy'] self.log.debug('The exact ground state energy is: {}'.format( results['energy'])) num_particles = self.molecule.num_alpha + self.molecule.num_beta two_qubit_reduction = True num_orbitals = self.qubit_op.num_qubits + \ (2 if two_qubit_reduction else 0) num_time_slices = 50 n_ancillae = 9 state_in = HartreeFock(self.qubit_op.num_qubits, num_orbitals, num_particles, qubit_mapping, two_qubit_reduction) iqft = Standard(n_ancillae) qpe = QPE(self.qubit_op, state_in, iqft, num_time_slices, n_ancillae, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) backend = get_aer_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) result = qpe.run(quantum_instance) self.log.debug('measurement results: {}'.format( result['measurements'])) self.log.debug('top result str label: {}'.format( result['top_measurement_label'])) self.log.debug('top result in decimal: {}'.format( result['top_measurement_decimal'])) self.log.debug('stretch: {}'.format( result['stretch'])) self.log.debug('translation: {}'.format( result['translation'])) self.log.debug('final energy from QPE: {}'.format(result['energy'])) self.log.debug('reference energy: {}'.format( self.reference_energy)) self.log.debug('ref energy (transformed): {}'.format( (self.reference_energy + result['translation']) * result['stretch'])) self.log.debug('ref binary str label: {}'.format( decimal_to_binary((self.reference_energy + result['translation']) * result['stretch'], max_num_digits=n_ancillae + 3, fractional_part_only=True))) np.testing.assert_approx_equal(result['energy'], self.reference_energy, significant=2)
# Build the qubit operator, which is the input to the VQE algorithm in Aqua ferOp = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) map_type = 'PARITY' qubitOp = ferOp.mapping(map_type) qubitOp = qubitOp.two_qubit_reduced_operator(num_particles) num_qubits = qubitOp.num_qubits # set the backend for the quantum computation from qiskit import Aer backend = Aer.get_backend('statevector_simulator') # setup a classical optimizer for VQE from qiskit_aqua.components.optimizers import L_BFGS_B optimizer = L_BFGS_B() # setup the initial state for the variational form from qiskit_chemistry.aqua_extensions.components.initial_states import HartreeFock init_state = HartreeFock(num_qubits, num_spin_orbitals, num_particles) # setup the variational form for VQE from qiskit_aqua.components.variational_forms import RYRZ var_form = RYRZ(num_qubits, initial_state=init_state) # setup and run VQE from qiskit_aqua.algorithms import VQE algorithm = VQE(qubitOp, var_form, optimizer) result = algorithm.run(backend) print(result['energy'])