Skip to content

Commit

Permalink
Option to load custom RE parameters in DF chemistry sample (#1662)
Browse files Browse the repository at this point in the history
This adds a program argument `-p` (long `--paramsfile`) to the Python
script of the DF chemistry sample to have the option to load one or
multiple RE configurations that are stored as objects or arrays in JSON
files. If no such argument is specified, the default configuration is
used as before.

This addresses the second bullet point in #1115.
  • Loading branch information
msoeken authored Jun 26, 2024
1 parent c682058 commit 70239b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
14 changes: 9 additions & 5 deletions samples/estimation/df-chemistry/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Resource Estimation for Double-factorized Chemistry

In this sample we evaluate the physical resource estimates of using the so-called double-factorized qubitization algorithm described in [[Phys. Rev. Research 3, 033055 (2021)](https://doi.org/10.1103/PhysRevResearch.3.033055)] to calculate the energy of a user provided Hamiltonian to chemical accuracy of 1 mHa.
In this sample we evaluate the physical resource estimates of using the so-called double-factorized qubitization algorithm described in [[Phys. Rev. Research 3, 033055 (2021)](https://doi.org/10.1103/PhysRevResearch.3.033055)] to calculate the energy of a user provided Hamiltonian to chemical accuracy of 1 mHa.

The Hamiltonian is provided as an FCIDUMP file that is available on your machine or can be downloaded via an HTTPS URL.

```
```text
usage: chemistry.py [-h] [-f FCIDUMPFILE]
Double-factorized chemistry sample
Expand All @@ -13,20 +13,24 @@ options:
-h, --help show this help message and exit
-f FCIDUMPFILE, --fcidumpfile FCIDUMPFILE
Path to the FCIDUMP file describing the Hamiltonian
-p [PARAMSFILE ...], --paramsfile [PARAMSFILE ...]
Optional parameter files to use for estimation
```

For example, the following command will download the FCIDUMP file `n2-10e-8o` to the working folder and run resource estimation for it:

```
```shell
chemistry.py -f https://aka.ms/fcidump/n2-10e-8o
```

After that, you can pass the path to the downloaded file to the script instead:

```
```shell
chemistry.py -f n2-10e-8o
```

By default, physical resources are estimates for a Majorana based qubit with 10⁻⁶ error rates (`qubit_maj_ns_e6`), a Floquet code QEC scheme. The error budget is set to 0.01 to reach the required chemical accuracy of 1 mHa. The `-p` program argument can be used to load other resource estimation parameters, specified in JSON files. The JSON files can either contain a JSON object for one configuration, or an array of JSON objects for multiple configurations. Make sure to set the error budget to 0.01 to guarantee the correct chemical accuracy.

You can choose some of the following URLs to download example files:

| URL | Instance name | Description |
Expand All @@ -37,4 +41,4 @@ You can choose some of the following URLs to download example files:
| https://aka.ms/fcidump/fe2s2-10e-40o | fe2s2-10e-40o | 10 electron, 40 orbital active space of [2Fe, 2S] cluster that is shown in [this paper](https://www.nature.com/articles/nchem.2041) |
| https://aka.ms/fcidump/polyyne-24e-24o | polyyne-24e-24o | 24 electron, 24 orbital active space of the polyyne molecule |

The numbers for the XVIII-cas4-fb-64e-56o instance roughly match the numbers in the paper [Assessing requirements for scaling quantum computers to real-world impact](https://aka.ms/AQ/RE/Paper), as we incorporated a few improvements in the implementation of the double-factorized chemistry algorithm as compared to the version used when the paper was published.
The numbers for the XVIII-cas4-fb-64e-56o instance roughly match the numbers in the paper [Assessing requirements for scaling quantum computers to real-world impact](https://aka.ms/AQ/RE/Paper), as we incorporated a few improvements in the implementation of the double-factorized chemistry algorithm as compared to the version used when the paper was published.
39 changes: 30 additions & 9 deletions samples/estimation/df-chemistry/chemistry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import math
import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -445,6 +446,12 @@ def ndarray2d_to_string(arr):
default="https://aka.ms/fcidump/n2-10e-8o",
help="Path to the FCIDUMP file describing the Hamiltonian",
)
parser.add_argument(
"-p",
"--paramsfile",
nargs="*",
help="Optional parameter files to use for estimation",
)
args = parser.parse_args()

# ----- Read the FCIDUMP file and get resource estimates from Q# algorithm -----
Expand Down Expand Up @@ -479,23 +486,37 @@ def ndarray2d_to_string(arr):
"Microsoft.Quantum.Applications.Chemistry.DoubleFactorizedChemistryParameters(0.001,))"
)

# Get resource estimates
res = qsharp.estimate(
qsharp_string,
params={
# Collect resource estimation parameters
if args.paramsfile is None:
params = {
"errorBudget": 0.01,
"qubitParams": {"name": "qubit_maj_ns_e6"},
"qecScheme": {"name": "floquet_code"},
},
}
else:
params = []
for paramsfile in args.paramsfile:
with open(paramsfile) as f:
data = json.load(f)
if isinstance(data, dict):
params.append(data)
else:
params += data

# Get resource estimates
res = qsharp.estimate(
qsharp_string,
params=params,
)

# Store estimates in json file
with open("resource_estimate.json", "w") as f:
f.write(res.json)

# Print high-level resource estimation results
print(f"Algorithm runtime: {res['physicalCountsFormatted']['runtime']}")
print(
f"Number of physical qubits required: {res['physicalCountsFormatted']['physicalQubits']}"
)
if "physicalCountsFormatted" in res:
print(f"Algorithm runtime: {res['physicalCountsFormatted']['runtime']}")
print(
f"Number of physical qubits required: {res['physicalCountsFormatted']['physicalQubits']}"
)
print("For more detailed resource counts, see file resource_estimate.json")

0 comments on commit 70239b2

Please sign in to comment.