Noiseless simulations
Let us take a look at how to encode a noiseless, yet time-dependent Hamiltonian of the following form:
where \(\alpha = \pi/2\) to represent a rotation of the \(\left|0\right>\) state of qubit \(0\) around the \(Y\) axis by \(\pi\) and \(\beta(t) = 1.27 * \sin(t)\) is some time-dependent function for \(\sigma^x_1 \sigma^x_2\) acting on qubit \(1\) and \(2\).
The code snippet below performs a simulation of the evolution of this \(H(t)\) for time tmax
= 1 both in SAMPLE
and OBS
mode:
import numpy as np
from qat.qpus import QutipQPU
from qat.core import Observable, Term, Schedule
from qat.core.variables import Variable, sin
# Define a time Variable
t = Variable("t", float)
# Define the Hamiltonian in a drive to enter the Schedule
alpha = np.pi / 2
beta_t = 1.27 * sin(t)
drive = [(alpha, Observable(3, pauli_terms=[Term(1, "Y", [0])])),
(beta_t, Observable(3, pauli_terms=[Term(1, "XX", [1, 2])]))]
schedule = Schedule(drive=drive, tmax=1.0) # tmax as the evolution duration from t=0
# Specify Observables to measure at the end
H_target_0 = Observable(3, pauli_terms=[Term(1, "Z", [0])])
H_target_12 = Observable(3, pauli_terms=[Term(1, "YX", [1, 2]),
Term(1.34, "ZZ", [1, 2])])
job_obs = schedule.to_job(job_type="OBS",
observable=H_target_0,
observables=[H_target_12])
# Jobs are in SAMPLE mode by default
job_sample = schedule.to_job()
# Create a QPU and send the jobs for simulation
qpu = QutipQPU()
res_obs = qpu.submit(job_obs)
res_sample = qpu.submit(job_sample)
# Output the results
print("<H_target_0> =", res_obs.value)
print("<H_target_12> =", res_obs.values[0])
for sample in res_sample:
if sample.probability > 1e-5:
print(sample.state, sample.amplitude)
[1/1] Cythonizing qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.pyx
running build_ext
building 'qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91' extension
creating build/temp.linux-x86_64-cpython-312
g++ -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall -I/usr/local/lib64/openmpi/../../include/openmpi-x86_64 -fPIC -I/var/lib/jenkins/workspace/orial-doc_release_myqlm-1.12.0_2/qat-tutorial/build_linux-x86_64_cpython_python312/venv/lib/python3.12/site-packages/qutip/core/data -I/var/lib/jenkins/workspace/orial-doc_release_myqlm-1.12.0_2/qat-tutorial/build_linux-x86_64_cpython_python312/venv/lib/python3.12/site-packages/numpy/_core/include -I/var/lib/jenkins/workspace/orial-doc_release_myqlm-1.12.0_2/qat-tutorial/build_linux-x86_64_cpython_python312/venv/include -I/usr/local/include/python3.12 -c qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.cpp -o build/temp.linux-x86_64-cpython-312/qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.o -w -O3 -funroll-loops
creating build/lib.linux-x86_64-cpython-312
g++ -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall -I/usr/local/lib64/openmpi/../../include/openmpi-x86_64 -shared -L/usr/local/lib64/openmpi/lib:-L/usr/local/cuda/lib64 -I/usr/local/lib64/openmpi/../../include/openmpi-x86_64 build/temp.linux-x86_64-cpython-312/qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.o -L/usr/local/lib -o build/lib.linux-x86_64-cpython-312/qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.cpython-312-x86_64-linux-gnu.so
copying build/lib.linux-x86_64-cpython-312/qtcoeff_557b8c38ee62aedf3ef06ba9ab1f91.cpython-312-x86_64-linux-gnu.so ->
Done
Did not find any quantum register info: wrapping results with a dummy BITSTRING register of length 0
Post processing a list of results of length 1
New batch of length 1 submitted
Compiling a batch of length 1
Starting compilation...
Returning compiled batch.
Resource management is not available, passing through.
Running jobs...
Done
Wrapping results using 1 quantum registers
Post processing a list of results of length 1
<H_target_0> = -0.9999999999998637
<H_target_12> = 0.42017582095668066
|100> (0.8343652765232267+0j)
|111> -0.5512119241564458j
H_target_0
, one should get \(-1\). At the same time, more involved observables can also be measured, e.g. H_target_12
on qubit \(1\) and \(2\), and furthermore, during the same simulation - via the observables
field of to_job()
.SAMPLE
mode the final state of the system will be shown. In this case the first qubit will have changed to state \(\left|1\right>\), while the second and the third will be in some superposition of the states \(\left|00\right>\) and \(\left|11\right>\).A more involved example with an imperfect QPU (for which we specify a hardware model) can be found in the following Getting started notebook. The section on analog computation gives a simple introduction to the class Schedule
along with examples on how to perform arithmetic and other operations with one or more instances of it.