-
Notifications
You must be signed in to change notification settings - Fork 1
Molecule Energy Simulator #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 10 commits
1f8fc9b
f59035d
8649ff2
7505df1
623c720
0a0a49a
36692a5
2409ecf
3a15690
24ec469
2b19648
e17bf15
7c4d29f
9198ebf
a07a76a
6fa2f8b
c19b3f2
5e3cb4b
cfe5e9c
bc474be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| from qiskit_nature.second_q.drivers import PySCFDriver | ||
| from qiskit_nature.second_q.mappers import ParityMapper | ||
| from qiskit_algorithms.optimizers import L_BFGS_B | ||
| from qiskit.primitives import Estimator | ||
| from qiskit_nature.second_q.circuit.library import HartreeFock, UCCSD | ||
| from qiskit_algorithms import VQE | ||
| from qiskit_nature.second_q.algorithms import GroundStateEigensolver | ||
| import sys | ||
|
|
||
| if len(sys.argv) != 2: | ||
| raise ValueError('This script expects exactly 1 argument: the molecule') | ||
| molecule = sys.argv[1] | ||
|
|
||
| driver = PySCFDriver(atom=molecule) | ||
| problem = driver.run() | ||
|
|
||
| mapper = ParityMapper(num_particles=problem.num_particles) | ||
|
|
||
| optimizer = L_BFGS_B() | ||
|
|
||
| estimator = Estimator() | ||
|
|
||
| ansatz = UCCSD( | ||
| problem.num_spatial_orbitals, | ||
| problem.num_particles, | ||
| mapper, | ||
| initial_state=HartreeFock( | ||
| problem.num_spatial_orbitals, | ||
| problem.num_particles, | ||
| mapper, | ||
| ), | ||
| ) | ||
|
|
||
| vqe = VQE(estimator, ansatz, optimizer) | ||
| vqe.initial_point = [0] * ansatz.num_parameters | ||
|
|
||
| algorithm = GroundStateEigensolver(mapper, vqe) | ||
|
|
||
| electronic_structure_result = algorithm.solve(problem) | ||
| electronic_structure_result.formatting_precision = 6 | ||
| print(electronic_structure_result) # we're going to calculate the total ground state energy | ||
|
|
||
|
|
||
| from qiskit_nature.second_q.mappers import JordanWignerMapper | ||
| from qiskit_algorithms.optimizers import SLSQP | ||
|
|
||
| driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.74279') | ||
| problem = driver.run() | ||
|
|
||
| mapper = JordanWignerMapper() | ||
|
|
||
| optimizer = SLSQP(maxiter=10000, ftol=1e-9) | ||
|
|
||
| estimator = Estimator() | ||
|
|
||
| from qiskit import QuantumCircuit | ||
| from qiskit.circuit.library import TwoLocal | ||
| import numpy as np | ||
|
|
||
| var_forms = [['ry', 'rz'], 'ry'] | ||
| entanglements = ['full', 'linear'] | ||
| entanglement_blocks = ['cx', 'cz', ['cx', 'cz']] | ||
| depths = list(range(1, 11)) | ||
|
|
||
| reference_circuit = QuantumCircuit(4) | ||
| reference_circuit.x(0) | ||
| reference_circuit.x(2) | ||
|
|
||
| results = np.zeros((len(depths), len(entanglements), len(var_forms), len(entanglement_blocks))) | ||
|
|
||
| for i, d in enumerate(depths): | ||
| print(f'Depth: {d} done.') | ||
| for j, e in enumerate(entanglements): | ||
| for k, vf in enumerate(var_forms): | ||
| for l, eb in enumerate(entanglement_blocks): | ||
| optimizer = SLSQP(maxiter=10000, ftol=1e-9) | ||
| variational_form = TwoLocal(4, rotation_blocks=vf, entanglement_blocks=eb,entanglement=e, reps=d) | ||
|
|
||
| ansatz = reference_circuit.compose(variational_form) | ||
|
|
||
| vqe = VQE(estimator, ansatz, optimizer) | ||
| vqe.initial_point = [0] * ansatz.num_parameters | ||
|
|
||
| algorithm = GroundStateEigensolver(mapper, vqe) | ||
|
|
||
| electronic_structure_result = algorithm.solve(problem) | ||
|
|
||
| results[i, j, k, l] = electronic_structure_result.total_energies[0] | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| from io import StringIO | ||
|
|
||
| fig1, axs1 = plt.subplots(2, 3, sharey=True, sharex=True) | ||
| fig2, axs2 = plt.subplots(2, 3, sharey=True, sharex=True) | ||
|
|
||
| fig1.supxlabel('Depth') | ||
| fig1.supylabel('Estimated ground state energy') | ||
| fig2.supxlabel('Depth') | ||
| fig2.supylabel('Estimated ground state energy') | ||
|
|
||
| for j, e in enumerate(entanglements): | ||
| for l, eb in enumerate(entanglement_blocks): | ||
| axs1[j, l].plot(depths, results[:, j, 0, l]) | ||
| axs1[j, l].set_title(f'{e}, ryrz, {eb}') | ||
| axs1[j, l].text(0.90, 0.75, f'Min: {np.min(results[:, j, 0, l]):.3f}') | ||
|
|
||
| for j, e in enumerate(entanglements): | ||
| for l, eb in enumerate(entanglement_blocks): | ||
| axs2[j, l].plot(depths, results[:, j, 1, l]) | ||
| axs2[j, l].set_title(f'{e}, ry, {eb}') | ||
| axs2[j, l].text(0.90, 0.75, f'Min: {np.min(results[:, j, 1, l]):.3f}') | ||
|
|
||
| def print_fig(fig): | ||
| string_io = StringIO() | ||
| fig.savefig(string_io, format='svg') | ||
| print(string_io.getvalue()) | ||
|
|
||
| print_fig(fig1) | ||
| print_fig(fig2) | ||
Elscrux marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # This file describes the python package requirements for all Qiskit demonstrator scripts | ||
| # supported by ProvideQ | ||
|
|
||
| # required for molecule energy solver | ||
| qiskit | ||
| qiskit_aer | ||
| qiskit_ibm_runtime | ||
| matplotlib | ||
| pylatexenc | ||
| qiskit-transpiler-service | ||
| qiskit-nature | ||
| qiskit-nature-pyscf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package edu.kit.provideq.toolbox.demonstrators; | ||
|
|
||
| import edu.kit.provideq.toolbox.Solution; | ||
| import edu.kit.provideq.toolbox.meta.SolvingProperties; | ||
| import edu.kit.provideq.toolbox.meta.SubRoutineResolver; | ||
| import edu.kit.provideq.toolbox.meta.setting.SolverSetting; | ||
| import edu.kit.provideq.toolbox.meta.setting.basic.TextSetting; | ||
| import edu.kit.provideq.toolbox.process.PythonProcessRunner; | ||
| import java.util.List; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.stereotype.Component; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * Demonstrator for the Molecule Energy simulation. | ||
| * Note that its python dependencies can only be installed on Linux and macOS. | ||
| * Based on this <a href="https://github.com/qiskit-community/qiskit-community-tutorials/blob/master/chemistry/h2_var_forms.ipynb">Jupyter Notebook</a> | ||
| */ | ||
| @Component | ||
| public class MoleculeEnergySimulator implements Demonstrator { | ||
| private final String scriptPath; | ||
| private final String venv; | ||
| private final ApplicationContext context; | ||
|
|
||
| private static final String SETTING_MOLECULE = "Molecule"; | ||
| private static final String DEFAULT_MOLECULE = "H .0 .0 .0; H .0 .0 0.74279"; | ||
|
|
||
| @Autowired | ||
| public MoleculeEnergySimulator( | ||
| @Value("${path.demonstrators.qiskit.molecule-energy}") String scriptPath, | ||
| @Value("${venv.demonstrators.qiskit.molecule-energy}") String venv, | ||
| ApplicationContext context) { | ||
| this.scriptPath = scriptPath; | ||
| this.venv = venv; | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "Molecule Energy Simulator"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Computes the ground state energy for a given molecule using VQE algorithm."; | ||
| } | ||
|
|
||
| @Override | ||
| public List<SolverSetting> getSolverSettings() { | ||
| return List.of( | ||
| new TextSetting( | ||
| false, | ||
| SETTING_MOLECULE, | ||
| "The molecule to be simulated in XYZ format - a di-hydrogen molecule by default", | ||
| DEFAULT_MOLECULE | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Solution<String>> solve(String input, SubRoutineResolver subRoutineResolver, | ||
| SolvingProperties properties) { | ||
| var solution = new Solution<>(this); | ||
|
|
||
| var molecule = properties.<TextSetting>getSetting(SETTING_MOLECULE) | ||
| .map(TextSetting::getText) | ||
| .orElse(DEFAULT_MOLECULE); | ||
|
|
||
| var processResult = context | ||
| .getBean(PythonProcessRunner.class, scriptPath, venv) | ||
| .withArguments(molecule) | ||
|
||
| .readOutputString() | ||
| .run(getProblemType(), solution.getId()); | ||
|
|
||
| return Mono.just(processResult.applyTo(solution)); | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.