Skip to content
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

Install dependencies and format hfvqe/quickstart #102

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 98 additions & 32 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 All @@ -40,7 +105,9 @@
"id": "09752498051c"
},
"source": [
"### Generate the input files, set up quantum resources, and set up the OpdmFunctional to make measurements. "
"## Set up the experiment\n",
"\n",
"Generate the input files, set up quantum resources, and set up the OpdmFunctional to make measurements. "
]
},
{
Expand All @@ -64,7 +131,8 @@
" constant=molecule.nuclear_repulsion,\n",
lamberta marked this conversation as resolved.
Show resolved Hide resolved
" 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 All @@ -76,8 +144,16 @@
"id": "61f9277513fe"
},
"source": [
"The displayed text is the output of the gradient based restricted Hartree-Fock. We define the gradient in `rhf_objective` and use the conjugate-gradient optimizer to optimize the basis rotation parameters. This is equivalent to doing Hartree-Fock theory from the canonical transformation perspective.\n",
"\n",
"The displayed text is the output of the gradient based restricted Hartree-Fock. We define the gradient in `rhf_objective` and use the conjugate-gradient optimizer to optimize the basis rotation parameters. This is equivalent to doing Hartree-Fock theory from the canonical transformation perspective."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "61f9277513fe"
},
"source": [
"## Estimate Quantities\n",
"\n",
"Next, we will do the following:\n",
"\n",
Expand All @@ -103,8 +179,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 +192,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 +219,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 +230,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 +239,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 +278,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 +289,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 +311,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 +331,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/*/*/*',
]},
)