דף זה טרם תורגם. התוכן מוצג באנגלית.
Build a function template for Hamiltonian simulation
This template encapsulates a workflow to simulate the time evolution of an initial state against a user defined spin-based Hamiltonian and returns a set of specified expectation values using the AQC addon.
This template is structured as a Qiskit pattern with the following steps:
1. Collecting input and mapping the problem
This section takes as an input the Hamiltonian to simulate, an initial state in the form of a QuantumCircuit, a set of observables to estimate expectation values, and a specification of options for the AQC addon. This step validates that all required input data is present and that they are in the correct format.
The input arguments are then used to construct the relevant quantum circuits and operators for the workflow. A target circuit is created and a matrix product state representation of this circuit is found using the AQC addon. Following this, an ansatz circuit is generated and optimized using tensor network methods, producing a final circuit which executes the remainder of the time evolution.
2. Prepare the generated circuits for execution
The generated circuits from the AQC addon are then transpiled to execute on a chosen backend. An EstimatorV2 instance is created with a default set of error mitigation options to manage the circuit execution.
3. Execution
Finally, the ansatz circuit is transpiled and executed on a QPU and collects estimates for all of the specified expectation values, which are returned in a serializable format for access by the user.
Write the function template
First, write a function template for Hamiltonian simulation that uses the AQC-Tensor Qiskit addon to map the problem description to a reduced-depth circuit for execution on hardware.
Throughout, the code is saved to ./source_files/template_hamiltonian_simulation.py. This file is the function template you can upload to and run remotely with Qiskit Serverless.
# Added by doQumentation — installs packages not in the Binder environment
%pip install -q mergedeep qiskit-addon-aqc-tensor qiskit-serverless quimb
# This cell is hidden from users, it just creates a new folder
from pathlib import Path
Path("./source_files").mkdir(exist_ok=True)
Collect and validate the inputs
Start by getting the inputs for the template. This example has domain-specific inputs relevant for Hamiltonian simulation (such as the Hamiltonian and observable) and capability-specific options (such as how much you want to compress the initial layers of the Trotter circuit using AQC-Tensor, or advanced options for fine-tuning error suppression and mitigation beyond the defaults that are part of this example).
%%writefile ./source_files/template_hamiltonian_simulation.py
from qiskit import QuantumCircuit
from qiskit_serverless import get_arguments, save_result
# Extract parameters from arguments
#
# Do this at the top of the program so it fails early if any required arguments are missing or invalid.
arguments = get_arguments()
dry_run = arguments.get("dry_run", False)
backend_name = arguments["backend_name"]
aqc_evolution_time = arguments["aqc_evolution_time"]
aqc_ansatz_num_trotter_steps = arguments["aqc_ansatz_num_trotter_steps"]
aqc_target_num_trotter_steps = arguments["aqc_target_num_trotter_steps"]
remainder_evolution_time = arguments["remainder_evolution_time"]
remainder_num_trotter_steps = arguments["remainder_num_trotter_steps"]
# Stop if this fidelity is achieved
aqc_stopping_fidelity = arguments.get("aqc_stopping_fidelity", 1.0)
# Stop after this number of iterations, even if stopping fidelity is not achieved
aqc_max_iterations = arguments.get("aqc_max_iterations", 500)
hamiltonian = arguments["hamiltonian"]
observable = arguments["observable"]
initial_state = arguments.get("initial_state", QuantumCircuit(hamiltonian.num_qubits))
Writing ./source_files/template_hamiltonian_simulation.py
%%writefile --append ./source_files/template_hamiltonian_simulation.py
import numpy as np
import json
from mergedeep import merge
# Configure `EstimatorOptions`, to control the parameters of the hardware experiment
#
# Set default options
estimator_default_options = {
"resilience": {
"measure_mitigation": True,
"zne_mitigation": True,
"zne": {
"amplifier": "gate_folding",
"noise_factors": [1, 2, 3],
"extrapolated_noise_factors": list(np.linspace(0, 3, 31)),
"extrapolator": ["exponential", "linear", "fallback"],
},
"measure_noise_learning": {
"num_randomizations": 512,
"shots_per_randomization": 512,
},
},
"twirling": {
"enable_gates": True,
"enable_measure": True,
"num_randomizations": 300,
"shots_per_randomization": 100,
"strategy": "active",
},
}
# Merge with user-provided options
estimator_options = merge(
arguments.get("estimator_options", {}), estimator_default_options
)
Appending to ./source_files/template_hamiltonian_simulation.py
When the function template is running, it is helpful to return information in the logs by using print statements, so that you can better evaluate the workload's progress. Following is a simple example of printing the estimator_options so there is a record of the actual Estimator options used. There are many more similar examples throughout the program to report progress during execution, including the value of the objective function during the iterative component of AQC-Tensor, and the two-qubit depth of the final instruction set architecture (ISA) circuit intended for execution on hardware.
%%writefile --append ./source_files/template_hamiltonian_simulation.py
print("estimator_options =", json.dumps(estimator_options, indent=4))
Appending to ./source_files/template_hamiltonian_simulation.py
Validate the inputs
An important aspect of ensuring that the template can be reused across a range of inputs is input validation. The following code is an example of verifying that the stopping fidelity during AQC-Tensor has been specified appropriately and if not, returning an informative error message for how to fix the error.
%%writefile --append ./source_files/template_hamiltonian_simulation.py
# Perform parameter validation
if not 0.0 < aqc_stopping_fidelity <= 1.0:
raise ValueError(
f"Invalid stopping fidelity: {aqc_stopping_fidelity}. It must be a positive float no greater than 1."
)
Appending to ./source_files/template_hamiltonian_simulation.py