Code
qcr:2606.85480.1

Projected Variational Quantum Dynamics (pVQD)

This Qiskit tutorial implements the projected Variational Quantum Dynamics (pVQD) algorithm, a variational method for simulating the real-time evolution of a quantum system on near-term hardware. Simulating how a quantum state evolves under a Hamiltonian is a central task in physics and chemistry, but exact (Trotterized) time evolution can require very deep circuits. pVQD instead keeps the state in a fixed, shallow parameterized ansatz and, at each small time step, variationally finds the new parameters that best reproduce the true infinitesimal evolution, by maximizing the overlap (fidelity) between the ansatz advanced one step and the previous state evolved by a short Trotter step. This keeps the circuit depth constant over the whole simulation, trading deep circuits for a sequence of small classical optimizations. The tutorial shows how to set up pVQD in Qiskit with a Hamiltonian, an ansatz, a time step, and the Estimator primitive, run the time evolution, and track observables along the trajectory. It explains the fidelity-based update and the role of gradients in the per-step optimization. It is a practical introduction to variational quantum dynamics, an approach well suited to studying time-dependent quantum phenomena on hardware-limited devices with Qiskit.
Quantum Simulation
Qubit
Circuit-based
Uploaded 1 day ago
9
Views
GitHub183
Citing this entry? Use this QCR ID
Uploaded by
QL
QCR Librarian

Overview

qiskit-community/qiskit-algorithms
18380
In [ ]:
# --- Setup cell added by QCR (not part of the original tutorial) ---
# Source: qiskit-community/qiskit-algorithms @ 0.4.0, Apache License 2.0.
# Installs the example's dependencies. If a later cell still reports a missing
# package, restart the runtime/kernel and run again from the top.
%pip install -q qiskit-algorithms==0.4.0 qiskit-aer matplotlib pylatexenc qiskit-algorithms

Projected Variational Quantum Dynamics

The projected Variational Quantum Dynamics (p-VQD) algorithm is a quantum algorithm for real time evolution. It's a variational algorithm that projects the state at time , as calculated with Trotterization, onto a parameterized quantum circuit.

For a quantum state constructed by a parameterized quantum circuit and a Hamiltonian , the update rule can be written as

where is calculated with a Trotter expansion (using e.g. the PauliEvolutionGate in Qiskit!).

The following tutorial explores the p-VQD algorithm, which is available as the PVQD class. For details on the algorithm, see the original paper: Barison et al. Quantum 5, 512 (2021).

The example we're looking at is the time evolution of the state under the Hamiltonian

which is an Ising Hamiltonian on two neighboring spins, up to a time , where we want to keep track of the total magnetization as an observable.

In [1]:
from qiskit.quantum_info import SparsePauliOp

final_time = 1
hamiltonian = SparsePauliOp.from_sparse_list(
    [
        ("ZZ", [0, 1], 0.1),
        ("X", [0], 1),
        ("X", [1], 1),
    ],
    num_qubits=2,
)
observable = SparsePauliOp(["ZI", "IZ"])

After defining our Hamiltonian and observable, we need to choose the parameterized ansatz we project the update steps onto. We have different choices here, but for real time evolution an ansatz that contains building blocks of the evolved Hamiltonian usually performs very well.

In [2]:
from qiskit.circuit import QuantumCircuit, ParameterVector

theta = ParameterVector(r"$\theta$", 5)
ansatz = QuantumCircuit(2)
ansatz.rx(theta[0], 0)
ansatz.rx(theta[1], 1)
ansatz.rzz(theta[2], 0, 1)
ansatz.rx(theta[3], 0)
ansatz.rx(theta[4], 1)

# you can try different circuits, like:
# from qiskit.circuit.library import efficient_su2
# ansatz = efficient_su2(2, reps=1)

ansatz.draw("mpl")

With this ansatz, the state is prepared if all parameters are 0. Hence we'll set the initial parameters to :

In [3]:
import numpy as np

initial_parameters = np.zeros(ansatz.num_parameters)

Before running the p-VQD algorithm, we need to select the backend and how we want to calculate the expectation values. Here, we'll perform exact statevector simulations (which are still very fast, as we investigate a 2 qubit system) through the reference primitive implementations found in qiskit.primitives.

In [4]:
from qiskit.primitives import StatevectorSampler, StatevectorEstimator
from qiskit_algorithms.state_fidelities import ComputeUncompute

# the fidelity is used to evaluate the objective: the overlap of the variational form and the trotter step
sampler = StatevectorSampler(default_shots=10_000, seed=42)
fidelity = ComputeUncompute(sampler)

# the estimator is used to evaluate the observables
estimator = StatevectorEstimator(seed=43)

Since p-VQD performs a classical optimization in each timestep to determine the best parameters for the projection, we also have to specify the classical optimizer. As a first example we're using BFGS, which typically works well in statevector simulations, but later we can switch to gradient descent.

In [5]:
from qiskit_algorithms.optimizers import L_BFGS_B

bfgs = L_BFGS_B()

Now we can define p-VQD and execute it!

In [6]:
from qiskit_algorithms import PVQD

pvqd = PVQD(
    fidelity, ansatz, initial_parameters, estimator=estimator, num_timesteps=100, optimizer=bfgs
)

The p-VQD implementation follows Qiskit's time evolution interface, thus we pack all information of the evolution problem into an input class: the hamiltonian under which we evolve the state, the final_time of the evolution and the observables (aux_operators) we keep track of.

In [7]:
from qiskit_algorithms import TimeEvolutionProblem

problem = TimeEvolutionProblem(
    hamiltonian, time=final_time, aux_operators=[hamiltonian, observable]
)

And then run the algorithm! Beware that for a large number of shots to compute the fidelity, running the algorithm might take a while.

In [8]:
result = pvqd.evolve(problem)
print(result)
{   'aux_ops_evaluated': array([ 0.20858505, -0.76148347]),
    'estimated_error': 0.0,
    'evolved_state': <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x7fd85d518c90>,
    'fidelities': [   1.0,
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0),
                      np.float64(1.0)],
    'observables': [   array([0.1, 2. ]),
                       array([0.10000507, 1.99960839]),
                       array([0.10002032, 1.99843373]),
                       array([0.1000458 , 1.99647649]),
                       array([0.10008166, 1.99373747]),
                       array([0.10012805, 1.99021777]),
                       array([0.1001852 , 1.98591882]),
                       array([0.10025338, 1.98084238]),
                       array([0.10033289, 1.97499049]),
                       array([0.1004241 , 1.96836554]),
                       array([0.10052742, 1.9609702 ]),
                       array([0.10064328, 1.95280749]),
                       array([0.10077218, 1.9438807 ]),
                       array([0.10091465, 1.93419347]),
                       array([0.10107126, 1.92374972]),
                       array([0.10124261, 1.91255369]),
                       array([0.10142934, 1.90060991]),
                       array([0.10163214, 1.88792324]),
                       array([0.10185169, 1.87449881]),
                       array([0.10208874, 1.86034208]),
                       array([0.10234404, 1.84545878]),
                       array([0.10261839, 1.82985494]),
                       array([0.10291258, 1.8135369 ]),
                       array([0.10322744, 1.79651128]),
                       array([0.10356381, 1.77878496]),
                       array([0.10392255, 1.76036515]),
                       array([0.10430451, 1.74125931]),
                       array([0.10471058, 1.72147517]),
                       array([0.10514162, 1.70102077]),
                       array([0.1055985, 1.6799044]),
                       array([0.10608212, 1.6581346 ]),
                       array([0.10659333, 1.6357202 ]),
                       array([0.10713299, 1.61267029]),
                       array([0.10770196, 1.5889942 ]),
                       array([0.10830107, 1.56470153]),
                       array([0.10893113, 1.53980211]),
                       array([0.10959294, 1.51430604]),
                       array([0.11028726, 1.48822364]),
                       array([0.11101485, 1.46156547]),
                       array([0.11177639, 1.43434232]),
                       array([0.11257258, 1.40656523]),
                       array([0.11340404, 1.37824544]),
                       array([0.11427136, 1.3493944 ]),
                       array([0.1151751, 1.3200238]),
                       array([0.11611575, 1.29014553]),
                       array([0.11709376, 1.25977168]),
                       array([0.11810953, 1.22891453]),
                       array([0.11916338, 1.19758657]),
                       array([0.1202556 , 1.16580048]),
                       array([0.12138639, 1.1335691 ]),
                       array([0.1225559 , 1.10090548]),
                       array([0.1237642 , 1.06782282]),
                       array([0.12501129, 1.0343345 ]),
                       array([0.12629712, 1.00045405]),
                       array([0.12762151, 0.96619516]),
                       array([0.12898426, 0.93157169]),
                       array([0.13038504, 0.89659762]),
                       array([0.13182347, 0.86128708]),
                       array([0.13329907, 0.82565432]),
                       array([0.13481126, 0.78971375]),
                       array([0.13635939, 0.75347987]),
                       array([0.13794272, 0.71696731]),
                       array([0.1395604, 0.6801908]),
                       array([0.14121149, 0.64316519]),
                       array([0.14289498, 0.60590542]),
                       array([0.14460974, 0.56842651]),
                       array([0.14635454, 0.53074357]),
                       array([0.14812806, 0.49287181]),
                       array([0.14992889, 0.45482649]),
                       array([0.15175552, 0.41662294]),
                       array([0.15360632, 0.37827656]),
                       array([0.15547959, 0.3398028 ]),
                       array([0.15737352, 0.30121714]),
                       array([0.15928619, 0.26253513]),
                       array([0.16121561, 0.22377233]),
                       array([0.16315967, 0.18494435]),
                       array([0.16511617, 0.14606682]),
                       array([0.16708282, 0.10715535]),
                       array([0.16905723, 0.06822562]),
                       array([0.17103693, 0.02929325]),
                       array([ 0.17301935, -0.00962609]),
                       array([ 0.17500183, -0.04851677]),
                       array([ 0.17698163, -0.08736318]),
                       array([ 0.17895592, -0.12614971]),
                       array([ 0.18092179, -0.16486079]),
                       array([ 0.18287626, -0.2034809 ]),
                       array([ 0.18481626, -0.24199454]),
                       array([ 0.18673864, -0.28038626]),
                       array([ 0.18864021, -0.31864067]),
                       array([ 0.19051768, -0.35674246]),
                       array([ 0.19236771, -0.39467635]),
                       array([ 0.1941869 , -0.43242715]),
                       array([ 0.19597178, -0.46997976]),
                       array([ 0.19771885, -0.50731916]),
                       array([ 0.19942454, -0.5444304 ]),
                       array([ 0.20108525, -0.58129865]),
                       array([ 0.20269731, -0.61790919]),
                       array([ 0.20425706, -0.65424738]),
                       array([ 0.20576077, -0.69029873]),
                       array([ 0.20720468, -0.72604885]),
                       array([ 0.20858505, -0.76148347])],
    'parameters': [   array([0., 0., 0., 0., 0.]),
                      array([0.00883928, 0.01155222, 0.00216917, 0.01123011, 0.00795315]),
                      array([0.01767857, 0.02310445, 0.00433835, 0.02246023, 0.0159063 ]),
                      array([0.02651785, 0.03465667, 0.00650752, 0.03369034, 0.02385945]),
                      array([0.03535714, 0.04620889, 0.0086767 , 0.04492046, 0.0318126 ]),
                      array([0.04419642, 0.05776111, 0.01084587, 0.05615057, 0.03976575]),
                      array([0.05303571, 0.06931334, 0.01301505, 0.06738069, 0.0477189 ]),
                      array([0.06187499, 0.08086556, 0.01518422, 0.0786108 , 0.05567206]),
                      array([0.07071428, 0.09241778, 0.0173534 , 0.08984092, 0.06362521]),
                      array([0.07955356, 0.10397   , 0.01952257, 0.10107103, 0.07157836]),
                      array([0.08839285, 0.11552223, 0.02169175, 0.11230115, 0.07953151]),
                      array([0.09723213, 0.12707445, 0.02386092, 0.12353126, 0.08748466]),
                      array([0.10607142, 0.13862667, 0.0260301 , 0.13476138, 0.09543781]),
                      array([0.1149107 , 0.15017889, 0.02819927, 0.14599149, 0.10339096]),
                      array([0.12374999, 0.16173112, 0.03036845, 0.15722161, 0.11134411]),
                      array([0.13258927, 0.17328334, 0.03253762, 0.16845172, 0.11929726]),
                      array([0.14142856, 0.18483556, 0.0347068 , 0.17968184, 0.12725041]),
                      array([0.15026784, 0.19638779, 0.03687597, 0.19091195, 0.13520356]),
                      array([0.15910713, 0.20794001, 0.03904515, 0.20214207, 0.14315671]),
                      array([0.16794641, 0.21949223, 0.04121432, 0.21337218, 0.15110986]),
                      array([0.1767857 , 0.23104445, 0.0433835 , 0.2246023 , 0.15906301]),
                      array([0.18562498, 0.24259668, 0.04555267, 0.23583241, 0.16701617]),
                      array([0.19446427, 0.2541489 , 0.04772185, 0.24706253, 0.17496932]),
                      array([0.20330355, 0.26570112, 0.04989102, 0.25829264, 0.18292247]),
                      array([0.21214284, 0.27725334, 0.0520602 , 0.26952276, 0.19087562]),
                      array([0.22098212, 0.28880557, 0.05422937, 0.28075287, 0.19882877]),
                      array([0.22982141, 0.30035779, 0.05639855, 0.29198299, 0.20678192]),
                      array([0.23866069, 0.31191001, 0.05856772, 0.3032131 , 0.21473507]),
                      array([0.24749998, 0.32346223, 0.0607369 , 0.31444322, 0.22268822]),
                      array([0.25633926, 0.33501446, 0.06290607, 0.32567333, 0.23064137]),
                      array([0.26517855, 0.34656668, 0.06507525, 0.33690345, 0.23859452]),
                      array([0.27401783, 0.3581189 , 0.06724442, 0.34813356, 0.24654767]),
                      array([0.28285712, 0.36967113, 0.0694136 , 0.35936368, 0.25450082]),
                      array([0.2916964 , 0.38122335, 0.07158277, 0.37059379, 0.26245397]),
                      array([0.30053569, 0.39277557, 0.07375195, 0.38182391, 0.27040713]),
                      array([0.30937497, 0.40432779, 0.07592112, 0.39305402, 0.27836028]),
                      array([0.31821426, 0.41588002, 0.0780903 , 0.40428414, 0.28631343]),
                      array([0.32705354, 0.42743224, 0.08025947, 0.41551425, 0.29426658]),
                      array([0.33589283, 0.43898446, 0.08242865, 0.42674436, 0.30221973]),
                      array([0.34473211, 0.45053668, 0.08459782, 0.43797448, 0.31017288]),
                      array([0.3535714 , 0.46208891, 0.086767  , 0.44920459, 0.31812603]),
                      array([0.36241068, 0.47364113, 0.08893617, 0.46043471, 0.32607918]),
                      array([0.37124997, 0.48519335, 0.09110535, 0.47166482, 0.33403233]),
                      array([0.38008925, 0.49674557, 0.09327452, 0.48289494, 0.34198548]),
                      array([0.38892854, 0.5082978 , 0.0954437 , 0.49412505, 0.34993863]),
                      array([0.39776782, 0.51985002, 0.09761287, 0.50535517, 0.35789178]),
                      array([0.40660711, 0.53140224, 0.09978205, 0.51658528, 0.36584493]),
                      array([0.41544639, 0.54295447, 0.10195122, 0.5278154 , 0.37379808]),
                      array([0.42428568, 0.55450669, 0.1041204 , 0.53904551, 0.38175124]),
                      array([0.43312496, 0.56605891, 0.10628957, 0.55027563, 0.38970439]),
                      array([0.44196424, 0.57761113, 0.10845875, 0.56150574, 0.39765754]),
                      array([0.45080353, 0.58916336, 0.11062792, 0.57273586, 0.40561069]),
                      array([0.45964281, 0.60071558, 0.1127971 , 0.58396597, 0.41356384]),
                      array([0.4684821 , 0.6122678 , 0.11496627, 0.59519609, 0.42151699]),
                      array([0.47732138, 0.62382002, 0.11713545, 0.6064262 , 0.42947014]),
                      array([0.48616067, 0.63537225, 0.11930462, 0.61765632, 0.43742329]),
                      array([0.49499995, 0.64692447, 0.1214738 , 0.62888643, 0.44537644]),
                      array([0.50383924, 0.65847669, 0.12364297, 0.64011655, 0.45332959]),
                      array([0.51267852, 0.67002891, 0.12581215, 0.65134666, 0.46128274]),
                      array([0.52151781, 0.68158114, 0.12798132, 0.66257678, 0.46923589]),
                      array([0.53035709, 0.69313336, 0.13015049, 0.67380689, 0.47718904]),
                      array([0.53919638, 0.70468558, 0.13231967, 0.68503701, 0.4851422 ]),
                      array([0.54803566, 0.71623781, 0.13448884, 0.69626712, 0.49309535]),
                      array([0.55687495, 0.72779003, 0.13665802, 0.70749724, 0.5010485 ]),
                      array([0.56571423, 0.73934225, 0.13882719, 0.71872735, 0.50900165]),
                      array([0.57455352, 0.75089447, 0.14099637, 0.72995747, 0.5169548 ]),
                      array([0.5833928 , 0.7624467 , 0.14316554, 0.74118758, 0.52490795]),
                      array([0.59223209, 0.77399892, 0.14533472, 0.7524177 , 0.5328611 ]),
                      array([0.60107137, 0.78555114, 0.14750389, 0.76364781, 0.54081425]),
                      array([0.60991066, 0.79710336, 0.14967307, 0.77487793, 0.5487674 ]),
                      array([0.61874994, 0.80865559, 0.15184224, 0.78610804, 0.55672055]),
                      array([0.62758923, 0.82020781, 0.15401142, 0.79733816, 0.5646737 ]),
                      array([0.63642851, 0.83176003, 0.15618059, 0.80856827, 0.57262685]),
                      array([0.6452678 , 0.84331225, 0.15834977, 0.81979839, 0.58058   ]),
                      array([0.65410708, 0.85486448, 0.16051894, 0.8310285 , 0.58853316]),
                      array([0.66294637, 0.8664167 , 0.16268812, 0.84225861, 0.59648631]),
                      array([0.67178565, 0.87796892, 0.16485729, 0.85348873, 0.60443946]),
                      array([0.68062494, 0.88952115, 0.16702647, 0.86471884, 0.61239261]),
                      array([0.68946422, 0.90107337, 0.16919564, 0.87594896, 0.62034576]),
                      array([0.69830351, 0.91262559, 0.17136482, 0.88717907, 0.62829891]),
                      array([0.70714279, 0.92417781, 0.17353399, 0.89840919, 0.63625206]),
                      array([0.71598208, 0.93573004, 0.17570317, 0.9096393 , 0.64420521]),
                      array([0.72482136, 0.94728226, 0.17787234, 0.92086942, 0.65215836]),
                      array([0.73366065, 0.95883448, 0.18004152, 0.93209953, 0.66011151]),
                      array([0.74249993, 0.9703867 , 0.18221069, 0.94332965, 0.66806466]),
                      array([0.75133922, 0.98193893, 0.18437987, 0.95455976, 0.67601781]),
                      array([0.7601785 , 0.99349115, 0.18654904, 0.96578988, 0.68397096]),
                      array([0.76901779, 1.00504337, 0.18871822, 0.97701999, 0.69192411]),
                      array([0.77785707, 1.01659559, 0.19088739, 0.98825011, 0.69987727]),
                      array([0.78669636, 1.02814782, 0.19305657, 0.99948022, 0.70783042]),
                      array([0.79553564, 1.03970004, 0.19522574, 1.01071034, 0.71578357]),
                      array([0.80437493, 1.05125226, 0.19739492, 1.02194045, 0.72373672]),
                      array([0.81321421, 1.06280449, 0.19956409, 1.03317057, 0.73168987]),
                      array([0.8220535 , 1.07435671, 0.20173327, 1.04440068, 0.73964302]),
                      array([0.83089278, 1.08590893, 0.20390244, 1.0556308 , 0.74759617]),
                      array([0.83973207, 1.09746115, 0.20607162, 1.06686091, 0.75554932]),
                      array([0.84857135, 1.10901338, 0.20824079, 1.07809103, 0.76350247]),
                      array([0.85741063, 1.1205656 , 0.21040997, 1.08932114, 0.77145562]),
                      array([0.86624992, 1.13211782, 0.21257914, 1.10055126, 0.77940877]),
                      array([0.8750892 , 1.14367004, 0.21474832, 1.11178137, 0.78736192]),
                      array([0.88392849, 1.15522227, 0.21691749, 1.12301149, 0.79531507])],
    'times': [   0.0,
                 0.01,
                 0.02,
                 0.03,
                 0.04,
                 0.05,
                 0.06,
                 0.07,
                 0.08,
                 0.09,
                 0.1,
                 0.11,
                 0.12,
                 0.13,
                 0.14,
                 0.15,
                 0.16,
                 0.17,
                 0.18,
                 0.19,
                 0.2,
                 0.21,
                 0.22,
                 0.23,
                 0.24,
                 0.25,
                 0.26,
                 0.27,
                 0.28,
                 0.29,
                 0.3,
                 0.31,
                 0.32,
                 0.33,
                 0.34,
                 0.35000000000000003,
                 0.36,
                 0.37,
                 0.38,
                 0.39,
                 0.4,
                 0.41000000000000003,
                 0.42,
                 0.43,
                 0.44,
                 0.45,
                 0.46,
                 0.47000000000000003,
                 0.48,
                 0.49,
                 0.5,
                 0.51,
                 0.52,
                 0.53,
                 0.54,
                 0.55,
                 0.56,
                 0.5700000000000001,
                 0.58,
                 0.59,
                 0.6,
                 0.61,
                 0.62,
                 0.63,
                 0.64,
                 0.65,
                 0.66,
                 0.67,
                 0.68,
                 0.6900000000000001,
                 0.7000000000000001,
                 0.71,
                 0.72,
                 0.73,
                 0.74,
                 0.75,
                 0.76,
                 0.77,
                 0.78,
                 0.79,
                 0.8,
                 0.81,
                 0.8200000000000001,
                 0.8300000000000001,
                 0.84,
                 0.85,
                 0.86,
                 0.87,
                 0.88,
                 0.89,
                 0.9,
                 0.91,
                 0.92,
                 0.93,
                 0.9400000000000001,
                 0.9500000000000001,
                 0.96,
                 0.97,
                 0.98,
                 0.99,
                 1.0]}

Now we can have a look at the results, which are stored in a PVQDResult object. This class has the fields

  • evolved_state: The quantum circuit with the parameters at the final evolution time.
  • times: The timesteps of the time integration. At these times we have the parameter values and evaluated the observables.
  • parameters: The parameter values at each timestep.
  • observables: The observable values at each timestep.
  • fidelities: The fidelity of projecting the Trotter timestep onto the variational form at each timestep.
  • estimated_error: The estimated error as product of all fidelities.

The energy should be constant in a real time evolution. However, we are projecting the time-evolved state onto a variational form, which might violate this rule.

In [9]:
import matplotlib.pyplot as plt

energies = np.real(result.observables)[:, 0]

plt.plot(result.times, energies, color="royalblue")
plt.xlabel("time $t$")
plt.ylabel("energy $E$")
plt.title("Energy over time")
Text(0.5, 1.0, 'Energy over time')

Since we also kept track of the total magnetization of the system, we can plot that quantity too. However let's first compute exact reference values to verify our algorithm results.

In [10]:
import scipy as sc


def exact(final_time, timestep, hamiltonian, initial_state):
    """Get the exact values for energy and the observable."""
    O = observable.to_matrix()
    H = hamiltonian.to_matrix()

    energ, magn = [], []  # list of energies and magnetizations evaluated at timesteps timestep
    times = []  # list of timepoints at which energy/obs are evaluated
    time = 0
    while time <= final_time:
        # get exact state at time t
        exact_state = initial_state.evolve(sc.linalg.expm(-1j * time * H))
        # store observables and time
        times.append(time)
        energ.append(exact_state.expectation_value(H).real)
        magn.append(exact_state.expectation_value(observable).real)

        # next timestep
        time += timestep

    return times, energ, magn
In [11]:
from qiskit.quantum_info import Statevector

initial_state = Statevector(ansatz.assign_parameters(initial_parameters))
exact_times, exact_energies, exact_magnetizations = exact(
    final_time, 0.01, hamiltonian, initial_state
)
In [12]:
magnetizations = np.real(result.observables)[:, 1]

plt.plot(result.times, magnetizations.real, color="crimson", label="PVQD")
plt.plot(exact_times, exact_magnetizations, ":", color="k", label="Exact")
plt.xlabel("time $t$")
plt.ylabel(r"magnetization $\langle Z_1 Z_2 \rangle$")
plt.title("Magnetization over time")
plt.legend(loc="best")
plt.show()

Looks pretty good!

Gradient-based optimizations

The PVQD class also implements parameter-shift gradients for the loss function and we can use a gradient descent optimization routine

Here we're using a learning rate of

and 80 optimization steps in each timestep.

In [13]:
from qiskit_algorithms.optimizers import GradientDescent

maxiter = 80
learning_rate = 0.1 * np.arange(1, maxiter + 1) ** (-0.602)
gd = GradientDescent(maxiter, lambda: iter(learning_rate))
In [14]:
pvqd.optimizer = gd

The following cell would take a few minutes to run for 100 timesteps, so we reduce them here.

In [15]:
n = 10
pvqd.num_timesteps = n
problem.time = 0.1
In [16]:
result_gd = pvqd.evolve(problem)
In [17]:
energies_gd = np.real(result_gd.observables)[:, 0]

plt.plot(result.times[: n + 1], energies[: n + 1], "-", color="royalblue", label="BFGS")
plt.plot(result_gd.times, energies_gd, "--", color="royalblue", label="Gradient descent")
plt.plot(exact_times[: n + 1], exact_energies[: n + 1], ":", color="k", label="Exact")
plt.legend(loc="best")
plt.xlabel("time $t$")
plt.ylabel("energy $E$")
plt.title("Energy over time")
Text(0.5, 1.0, 'Energy over time')

We can observe here, that the energy does vary quite a bit! But as we mentioned before, p-VQD does not preserve the energy.

In [18]:
magnetizations_gd = np.real(result_gd.observables)[:, 1]

plt.plot(result.times[: n + 1], magnetizations[: n + 1], "-", color="crimson", label="BFGS")
plt.plot(result_gd.times, magnetizations_gd, "--", color="crimson", label="Gradient descent")
plt.plot(exact_times[: n + 1], exact_magnetizations[: n + 1], ":", color="k", label="Exact")
plt.legend(loc="best")
plt.xlabel("time $t$")
plt.ylabel(r"magnetization $\langle Z_1 + Z_2 \rangle$")
plt.title("Magnetization over time")
Text(0.5, 1.0, 'Magnetization over time')

The magnetization, however, is computed very precisely.

In [19]:
import tutorial_magics

%qiskit_version_table
%qiskit_copyright

Version Information

SoftwareVersion
qiskit2.0.2
qiskit_algorithms0.4.0
System information
Python version3.13.3
OSLinux
Tue Jun 17 10:31:27 2025 CEST

This code is a part of a Qiskit project

© Copyright IBM 2017, 2025.

This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.

Join the Discussion

Comments (0)

No comments yet. Be the first to share your thoughts!

Indexed by QCR Librarian

This entry was created automatically from publicly available records. QCR links to public sources and only stores repository content where the license permits redistribution.

Versions

v1 Latest
Jun 17, 2026
qcr:2606.85480.1

Cite all versions? Use the base QCR ID to always reference the latest version of this entry.

Tools used

Qiskit

Keywords

pvqd
time-evolution
variational
quantum-dynamics
qiskit

You may also like5