Project for Quantathon
Given a Hamiltonian
+0.00698131079425246 * IIIZ
-0.0004978294000830275 * IIZI
+4.664512584628966e-05 * IZII
+0.0004303465157577957 * ZIII
+0.5099539391488543 * IIZZ
+0.5099677387273946 * IZIZ
+0.5099488492845516 * IZZI
+0.5099106232913859 * ZIIZ
+0.5099467089998899 * ZIZI
+0.5099046167492709 * ZZII
and the ansatz
ry = TwoLocal(num_assets, "ry", "cz", reps=3, entanglement="full")
Find the following using SamplingVQE and EsimatorVQE for every step of the iteration
Eigenstates, corresponding eigenvalues, and their probability.
Also, extract the variational parameters associated with every step of the iteration.
Here's an explanation of the key components of the code:
Importing Libraries: The code starts by importing necessary libraries and modules, including FastAPI for creating the API, Pydantic for defining data models, Qiskit for quantum computing, and other relevant dependencies.
Data Models:
PauliTerm
andHamiltonianInput
are Pydantic models used for validating the input data.PauliTerm
represents a single term in the Hamiltonian, including its coefficient and operator.HamiltonianInput
is a list ofPauliTerm
instances, representing the entire Hamiltonian.
CalculationStatus Model:
- Another Pydantic model,
CalculationStatus
, is defined to represent the status of the calculation for each iteration. It includes fields for the iteration number and the status.
FastAPI Setup:
- An instance of FastAPI is created as
app
.
API Endpoint:
- The
/eigen
endpoint is defined to receive a POST request. It expects a JSON payload withHamiltonianInput
data and an optionaliterations
parameter (default is 5).
Processing Hamiltonian:
- The code processes the Hamiltonian input by converting it into a Qiskit
PauliOp
. It iterates through the terms in the Hamiltonian, parses the operator strings, and builds the corresponding Pauli operators (I, X, Y, Z) for each term. These Pauli operators are then combined to create thehamiltonian_op
.
Ansatz Setup:
- The code sets up an ansatz circuit using
TwoLocal
. It uses the "ry" (Ry) rotation gate and "cz" (Controlled-Z) entanglement, with a specified number of repetitions.
Backend and Optimizer:
- It defines the quantum backend as the Aer simulator and selects the SPSA optimizer with a maximum of 50 iterations.
VQE Initialization:
- The Variational Quantum Eigensolver (VQE) is initialized with the ansatz, optimizer, and quantum instance.
Iteration and Calculation:
- The code then enters a loop for the specified number of iterations. Inside each iteration, it computes the minimum eigenvalue of the Hamiltonian using VQE. It retrieves the eigenstate, eigenvalue, and optimal variational parameters associated with the result.
pip3 install -r requirements.txt
cd frontend
npm install
# Deployment
cd backend
uvicorn main:app
# Development
uvicorn main:app --reload
# Deployment
cd frontend
npm run build
npm start
# Development
npm run dev