Measurement¶
Measuring position¶
Real quantum system are measurable. This property and physical effect is complicated to model with just pen and paper, since it relies on a component of 'dice-throwing'. Computationally, using a Metropolis-Hastings random walk algorithm, the process of measurement becomes instead rather straightforward.
BraketLab models measurable kets, meaning you may do as follows:
import braketlab as bk
import matplotlib.pyplot as plt
import numpy as np
p = bk.basisbank.get_harmonic_oscillator_function(5)
m = p.measure(repetitions = 10000) # measure position, repeat experiment 10000 times with reset
# visualize results
population, bins = np.histogram(m, bins = 61)
plt.figure()
plt.plot(.5*(bins[1:]+bins[:-1]), population, ".")
plt.xlabel("Position")
plt.ylabel("Population")
plt.show()
Arbitrary measurements¶
Disclaimer: this feature is still a on the experimental stage, and will be more tested in the future.
If you provide the measurement-method with a Hermitian operator with it's eigenstates, the outputs will distribute correspondngly with a projection onto the eigenstates of the operator. (This feature presupposes, however, that the operator has a set of eigenfunctions, which currently no operators in BraketLab posess by default. But feel free to experiment.
In the following example, we create the Hamiltonian of the Harmonic Oscillator with a truncated set of eigenfunctions. We thereafter measure the energy.
x = bk.get_default_variables(0, 1)[0]
n = 2
psi = bk.basisbank.get_harmonic_oscillator_function(2)
Hpsi = (bk.get_kinetic_operator() *psi + .5*x**2*psi)
E_n = psi.bra@Hpsi
print("Energy %i = %f" % (n, E_n))
Energy 2 = 2.500000
H = bk.get_kinetic_operator() #making a dummy operator
eigenstates = []
eigenvalues = []
for n in range(6):
eigenstates.append(bk.basisbank.get_harmonic_oscillator_function(n))
eigenvalues.append(eigenstates[-1].bra@(bk.get_kinetic_operator() *eigenstates[-1] + .5*x**2*eigenstates[-1]))
H.eigenstates = np.array(eigenstates)
H.eigenvalues = np.array(eigenvalues)
#psi.measure(observable = H, repetitions = 100 )
print("Eigenvalues:", H.eigenvalues)
print("Measurement outcome:", psi.measure(observable = H, repetitions = 100 ))
Eigenvalues: [0.5 1.5 2.5 3.5 4.5 5.5] [3.15544362e-30 0.00000000e+00 1.00000000e+00 0.00000000e+00 5.89816045e-34 0.00000000e+00] stepsize: 0.08333333333333333 Measurement outcome: [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5]
Let's also measure the energy of another distribution in the harmonic oscillator potential:
import sympy as sp
psi_d = bk.ket( sp.exp(-.1*(x-1.5)**2) )
psi_d = psi_d*(psi_d.bra@psi_d)**-.5
print("Measurement outcome:", psi_d.measure(observable = H, repetitions = 100 ))
[0.51227518 0.0640344 0.16053068 0.04819255 0.07297033 0.03014132] stepsize: 0.08333333333333333 Measurement outcome: [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5]