Skip to content

Commit

Permalink
Install dependencies and format hfvqe/quickstart
Browse files Browse the repository at this point in the history
Partially address quantumlib#101. Re-work `molecular_exmaple.py` to
support putting these data files elsewhere in the future.
  • Loading branch information
mpharrigan committed Nov 20, 2020
1 parent 7eb5b64 commit b5e707f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 93 deletions.
114 changes: 85 additions & 29 deletions docs/hfvqe/quickstart.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "IafxybMjKfBO"
},
"source": [
"##### Copyright 2020 Google"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "pc1aHcGvKmHe"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -11,6 +42,42 @@
"This code tutorial shows how to estimate a 1-RDM and perform variational optimization"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FQEYY3gnK51d"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.example.org/cirq/experiments/hfvqe/quickstart\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/ReCirq/blob/master/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/quantumlib/ReCirq/blob/master/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/ReCirq/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8625de3d3c0d"
},
"outputs": [],
"source": [
"try:\n",
" import recirq\n",
"except ImportError:\n",
" !pip install --quiet git+https://github.com/quantumlib/ReCirq"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -19,19 +86,17 @@
},
"outputs": [],
"source": [
"# Import library functions and define a helper function\n",
"import numpy as np\n",
"import cirq\n",
"\n",
"from recirq.hfvqe.gradient_hf import rhf_func_generator\n",
"from recirq.hfvqe.opdm_functionals import OpdmFunctional\n",
"from recirq.hfvqe.analysis import (compute_opdm,\n",
" mcweeny_purification,\n",
" resample_opdm,\n",
" fidelity_witness,\n",
" fidelity)\n",
"from recirq.hfvqe.analysis import (\n",
" compute_opdm, mcweeny_purification,\n",
" resample_opdm, fidelity_witness,\n",
" fidelity)\n",
"from recirq.hfvqe.third_party.higham import fixed_trace_positive_projection\n",
"from recirq.hfvqe.molecular_example import make_h6_1_3\n"
"from recirq.hfvqe.molecular_example import make_h6_1_3"
]
},
{
Expand Down Expand Up @@ -64,7 +129,8 @@
" constant=molecule.nuclear_repulsion,\n",
" one_body_integrals=obi,\n",
" two_body_integrals=tbi,\n",
" num_electrons=molecule.n_electrons // 2, # only simulate spin-up electrons\n",
" # only simulate spin-up electrons:\n",
" num_electrons=molecule.n_electrons // 2,\n",
" clean_xxyy=True,\n",
" purification=True\n",
" )"
Expand Down Expand Up @@ -103,8 +169,7 @@
"measurement_data = opdm_func.calculate_data(parameters)\n",
"\n",
"# 2.\n",
"opdm, var_dict = compute_opdm(measurement_data,\n",
" return_variance=True)\n",
"opdm, var_dict = compute_opdm(measurement_data, return_variance=True)\n",
"opdm_pure = mcweeny_purification(opdm)\n",
"\n",
"# 3.\n",
Expand All @@ -117,7 +182,9 @@
"nocc = molecule.n_electrons // 2\n",
"nvirt = molecule.n_orbitals - nocc\n",
"initial_fock_state = [1] * nocc + [0] * nvirt\n",
"for _ in range(1000): # 1000 repetitions of the measurement\n",
"\n",
"# 1000 repetitions of the measurement\n",
"for _ in range(1000): \n",
" new_opdm = resample_opdm(opdm, var_dict)\n",
" raw_energies.append(opdm_func.energy_from_opdm(new_opdm))\n",
" raw_fidelity_witness.append(\n",
Expand All @@ -142,7 +209,6 @@
" fidelity(target_unitary=true_unitary,\n",
" measured_opdm=new_opdm_pure)\n",
" )\n",
"print('\\n\\n\\n\\n')\n",
"print(\"Canonical Hartree-Fock energy \", molecule.hf_energy)\n",
"print(\"True energy \", energy(parameters))\n",
"print(\"Raw energy \", opdm_func.energy_from_opdm(opdm),\n",
Expand All @@ -154,7 +220,7 @@
"print(\"Purified fidelity witness \", np.mean(purified_fidelity_witness).real,\n",
" \"+- \", np.std(purified_fidelity_witness))\n",
"print(\"Purified fidelity \", np.mean(purified_fidelity).real,\n",
" \"+- \", np.std(purified_fidelity))\n"
" \"+- \", np.std(purified_fidelity))"
]
},
{
Expand All @@ -163,7 +229,7 @@
"id": "669a320cb246"
},
"source": [
"This should print out the various energies estimated from the 1-RDM along with error bars. Generated from resampling the 1-RDM based on the estimated covariance."
"This prints out the various energies estimated from the 1-RDM along with error bars. Generated from resampling the 1-RDM based on the estimated covariance."
]
},
{
Expand Down Expand Up @@ -202,7 +268,7 @@
"source": [
"from recirq.hfvqe.mfopt import moving_frame_augmented_hessian_optimizer\n",
"from recirq.hfvqe.opdm_functionals import RDMGenerator\n",
"import matplotlib.pyplot as plt\n",
"\n",
"rdm_generator = RDMGenerator(opdm_func, purification=True)\n",
"opdm_generator = rdm_generator.opdm_generator\n",
"\n",
Expand All @@ -213,7 +279,7 @@
" verbose=True, delta=0.03,\n",
" max_iter=20,\n",
" hessian_update='diagonal',\n",
" rtol=0.50E-2)\n"
" rtol=0.50E-2)"
]
},
{
Expand All @@ -235,13 +301,15 @@
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.semilogy(range(len(result.func_vals)),\n",
" np.abs(np.array(result.func_vals) - energy(parameters)),\n",
" 'C0o-')\n",
"plt.xlabel(\"Optimization Iterations\", fontsize=18)\n",
"plt.ylabel(r\"$|E - E^{*}|$\", fontsize=18)\n",
"plt.tight_layout()\n",
"plt.show()\n"
"plt.show()"
]
}
],
Expand All @@ -253,18 +321,6 @@
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
}
},
"nbformat": 4,
Expand Down
3 changes: 1 addition & 2 deletions recirq/hfvqe/analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
from recirq.hfvqe.analysis import (trace_distance, kdelta, energy_from_opdm,
fidelity_witness, fidelity,
mcweeny_purification)
from recirq.hfvqe.molecular_example import make_h6_1_3
from recirq.hfvqe.molecular_example_odd_qubits import (make_h3_2_5)
from recirq.hfvqe.molecular_example import make_h6_1_3, make_h3_2_5
from recirq.hfvqe.gradient_hf import rhf_func_generator


Expand Down
45 changes: 37 additions & 8 deletions recirq/hfvqe/molecular_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
from recirq.hfvqe.objective import (RestrictedHartreeFockObjective,
generate_hamiltonian)

_MOLECULAR_DATA_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) + '/molecular_data'

def make_h6_1_3() -> Tuple[RestrictedHartreeFockObjective, of.MolecularData, np.
ndarray, np.ndarray, np.ndarray]:
# load the molecule from moelcular data
import recirq.hfvqe as hfvqe
h6_1_3_path = os.path.join(
hfvqe.__path__[0],
'molecular_data/hydrogen_chains/h_6_sto-3g/bond_distance_1.3')

molfile = os.path.join(h6_1_3_path, 'H6_sto-3g_singlet_linear_r-1.3.hdf5')
def make_h6_1_3(molecular_data_directory=None) \
-> Tuple[RestrictedHartreeFockObjective, of.MolecularData,
np.ndarray, np.ndarray, np.ndarray]:
if molecular_data_directory is None:
molecular_data_directory = _MOLECULAR_DATA_DIRECTORY

h6_1_3_path = f'{molecular_data_directory}/hydrogen_chains/h_6_sto-3g/bond_distance_1.3'
molfile = f'{h6_1_3_path}/H6_sto-3g_singlet_linear_r-1.3.hdf5'
molecule = of.MolecularData(filename=molfile)
molecule.load()

Expand All @@ -52,3 +53,31 @@ def make_h6_1_3() -> Tuple[RestrictedHartreeFockObjective, of.MolecularData, np.
scipy_result = rhf_minimization(rhf_objective)

return rhf_objective, molecule, scipy_result.x, obi, tbi


def make_h3_2_5(molecular_data_directory=None) \
-> Tuple[RestrictedHartreeFockObjective, of.MolecularData,
np.ndarray, np.ndarray, np.ndarray]:
if molecular_data_directory is None:
molecular_data_directory = _MOLECULAR_DATA_DIRECTORY

h3_2_5_path = f'{molecular_data_directory}/hydrogen_chains/h_3_p_sto-3g/bond_distance_2.5'
molfile = f'{h3_2_5_path}/H3_plus_sto-3g_singlet_linear_r-2.5.hdf5'
molecule = of.MolecularData(filename=molfile)
molecule.load()

S = np.load(os.path.join(h3_2_5_path, 'overlap.npy'))
Hcore = np.load(os.path.join(h3_2_5_path, 'h_core.npy'))
TEI = np.load(os.path.join(h3_2_5_path, 'tei.npy'))

_, X = sp.linalg.eigh(Hcore, S)
obi = of.general_basis_change(Hcore, X, (1, 0))
tbi = np.einsum('psqr', of.general_basis_change(TEI, X, (1, 0, 1, 0)))
molecular_hamiltonian = generate_hamiltonian(obi, tbi,
molecule.nuclear_repulsion)

rhf_objective = RestrictedHartreeFockObjective(molecular_hamiltonian,
molecule.n_electrons)

scipy_result = rhf_minimization(rhf_objective)
return rhf_objective, molecule, scipy_result.x, obi, tbi
54 changes: 0 additions & 54 deletions recirq/hfvqe/molecular_example_odd_qubits.py

This file was deleted.

4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
description="",
long_description=open('README.md', encoding='utf-8').read(),
packages=find_packages(),
package_data={'recirq': [
# https://github.com/quantumlib/ReCirq/issues/101
'hfvqe/molecular_data/hydrogen_chains/*/*/*',
]},
)

0 comments on commit b5e707f

Please sign in to comment.