bolt
is a *very fast* library that allows one to simulate and optimize interferometers at the quantum level.
bolt
does its magic by computing only the input-output amplitudes of the interferometer that are needed, rather than computing the entire transformation tensor up to a given Fock space cutoff N
(i.e. all of the amplitudes).
Then, it performs the gradient optimization in the Lie algebra of the unitary group, which allows it to update the covariance matrix directly, without worrying about decomposing the interferometer into some arrangement of beam splitters and phase shifters.
It also (optionally) implements the natural gradient in the Orthogonal group, for an even faster convergence.
The State
class is a dictionary of ket:amplitude pairs:
from bolt import State, IOSpec
_in = State({(1,1,1,0,0,0):1.0}) # |1,1,1,0,0,0>
_out = State({(1,0,1,0,1,0):1.0}) # |1,0,1,0,1,0>
# IOSpec is an input-output relation (pure state in -> pure state out)
io = IOSpec(input_state = _in, output_state = _out)
The Requirements
class collects all of the required input-output relations that we require from the interferometer.
Generally, an interferometer that satisfies all of the required relations does not exist, but the optimizer will try to find one
that satisfies them with the highest probability.
from bolt import Requirements
# format: {IOSpec:weight, etc...}
req = Requirements({io:1.0})
Note that the first time the optimizer is called, the various numba
functions in the code are compiled.
Subsequent calls will start immediately, until you restart the ipython kernel.
from bolt import Optimizer
opt = Optimizer(lr = 0.01, max_steps=500)
cov_matrix = opt(req)
print(f'The search took {opt.elapsed:.3f} seconds')
import matplotlib.pyplot as plt
plt.plot(opt.losses)
Let's increase the complexity (16 modes, 12 photons). It should still be reasonably fast (44 it/s on my laptop):
from bolt import State, IOSpec, Requirements, Optimizer
_in = State({(1,1,3,1,2,0,0,0,0,0,0,3,0,0,1,0):1.0}) # |1,1,3,1,2,0,0,0,0,0,0,3,0,0,1,0>
_out = State({(1,0,1,0,2,0,1,2,1,0,0,2,0,1,1,0):1.0}) # |1,0,1,0,2,0,1,2,1,0,0,2,0,1,1,0>
io = IOSpec(input_state = _in, output_state = _out)
req = Requirements({io:1.0})
opt = Optimizer(lr = 0.02)
cov_matrix = opt(req)
Bolt
can generate the complete output state as well:
from bolt.utils import all_outputs
from scipy.stats import unitary_group
from bolt import State
state_in = State({(0,8,0,8):np.sqrt(1/4), (8,0,8,0):np.sqrt(1/4), (8,0,0,8):np.sqrt(1/4), (0,8,8,0):np.sqrt(1/4)})
V = unitary_group.rvs(state_in.num_modes) # interferometer unitary
out,_ = all_outputs(state_in, V, grad=False)
Note that at times the optimizer gets stuck in a local minimum. Run the optimization a few times to assess how often this happens.
States from Eq. (2) in PRA 94, 042331 (2011).
import numpy as np
from bolt import State, IOSpec, Requirements, Optimizer
psip = State({(1,0,0,1):np.sqrt(1/2), (0,1,1,0):np.sqrt(1/2)})
psim = State({(1,0,0,1):np.sqrt(1/2), (0,1,1,0):-np.sqrt(1/2)})
phip = State({(1,0,1,0):np.sqrt(1/2), (0,1,0,1):np.sqrt(1/2)})
phim = State({(1,0,1,0):np.sqrt(1/2), (0,1,0,1):-np.sqrt(1/2)})
io1 = IOSpec(psip, State({(1,1,0,0):np.sqrt(1/2), (0,0,1,1):np.sqrt(1/2)}))
io2 = IOSpec(psim, State({(1,0,0,1):np.sqrt(1/2), (0,1,1,0):-np.sqrt(1/2)}))
io3 = IOSpec(phip, State({(2,0,0,0):1/2, (0,2,0,0):1/2, (0,0,2,0):1/2, (0,0,0,2):1/2}))
io4 = IOSpec(phim, State({(2,0,0,0):1/2, (0,0,2,0):1/2, (0,2,0,0):-1/2, (0,0,0,2):-1/2}))
req = Requirements({io1:1.0, io2:1.0, io3:1.0, io4:1.0})
opt = Optimizer(lr = 0.01)
cov_matrix = opt(req)
print(f'The search took {opt.elapsed:.3f} seconds')
import matplotlib.pyplot as plt
plt.plot(opt.losses);
Here we find out that we can generate a GHZ state with probability 1/2.
import numpy as np
import matplotlib.pyplot as plt
from bolt import State, IOSpec, Requirements, Optimizer
in_111 = State({(1,1,1,0,0,0):1.0})
out_GHZ = State({(1,0,1,0,1,0):np.sqrt(1/2), (0,1,0,1,0,1):np.sqrt(1/2)})
io = IOSpec(in_111, out_GHZ)
req = Requirements({io:1.0})
opt = Optimizer(lr = 0.01)
cov_matrix = opt(req)
print(f'The search took {opt.elapsed:.3f} seconds')
plt.plot(opt.losses);
Let's push the limits with 11 photons and 22 modes, while using the natural gradient. Compare with the Lie Algebra implementation and notice the difference. Note that the natural gradient may be a bit sensible to the learning rate.
import numpy as np
import matplotlib.pyplot as plt
from bolt import State, IOSpec, Requirements, Optimizer
p = 11
in_ = State({(1,)*p + (0,)*p:1.0})
out_GHZ = State({(1,0)*p:np.sqrt(1/2), (0,1)*p:np.sqrt(1/2)})
io = IOSpec(in_, out_GHZ)
req = Requirements({io:1.0})
opt = Optimizer(lr = 0.02, natural=True) # change to natural=False for Lie Algebra implementation
cov_matrix = opt(req)
print(f'The search took {opt.elapsed:.3f} seconds')
plt.plot(opt.losses[1:]);
Sometimes, you are not aiming to find out a optimized interferometer and you only want to find out with a setup interferometer, which state will appear on the output state and what is the probability of it. You can then try with this:
from bolt import Interferometer
from bolt import State
import numpy as np
#Alternative interferometer you can define as you wish
S2 = 1/2*np.array([[1,1j,1j,-1],[1j,1,-1,1j],[1j,-1,1,1j],[-1,1j,1j,1]],dtype=np.complex128)
#Alternative input state
state_in = State({(2,0,0,2):np.sqrt(1/2),(0,2,2,0):np.sqrt(1/2)})
#Define your Interferometer
intf = Interferometer(S2)
#Get your results!
intf.getalloutputs(state_in)