Skip to content

Commit

Permalink
hot fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kareem1925 committed Feb 29, 2024
1 parent 938249d commit f70cf2e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 24 deletions.
27 changes: 27 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from quasi.simulation import Simulation
from quasi.experiement import Experiement
from quasi.components.gates import Squeezing, Beamsplitter, Phase
from math import pi
import numpy as np
from quasi._math.fock import mean_photon
from quasi._math.fock.ops import fock_state, tensor, vacuumStateMixed, adagger, apply_gate_BLAS, fock_operator


squeezing_gate = Squeezing(r=0.1, phi=pi, cutoff=3)

bs_gate = Beamsplitter(theta=pi/4, phi=pi/2, cutoff=3)

phase_gate = Phase(phi=pi/4, cutoff=10)

exp_1 = Experiement(num_modes=3, cutoff=3)

exp_1.state_init(1, modes=[0])

#exp_1.add_operation(bs_gate.get_operator().transpose((0, 2, 1, 3)), [0, 1])
#exp_1.add_operation(squeezing_gate.get_operator(), [0])

exp_1.execute()

state = exp_1.state
print(state.dm().shape)

4 changes: 2 additions & 2 deletions examples/hong_ou_mandel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from quasi.simulation import Simulation
from quasi.experiement import Experiement
from quasi.experiment import Experiment
from quasi.components.gates import Squeezing, Beamsplitter
from math import pi

Expand All @@ -14,7 +14,7 @@
bs_gate = Beamsplitter(theta=0.5, phi=pi / 2)


exp_1 = Experiement()
exp_1 = Experiment()

exp_1.state_init("type of state")
exp_1.add_operation(squeezing_gate, modes=0)
Expand Down
5 changes: 3 additions & 2 deletions quasi/_math/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .fock.ops import (
displacement,
phase,
mean_photon_number,
mean_photon,
squeezing,
adagger,
a,
Expand All @@ -20,4 +20,5 @@
cubicPhase,
lossChannel,
tensor,
)
fock_operator,
)
3 changes: 2 additions & 1 deletion quasi/_math/fock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
coherent_state,
adagger,
a,
mean_photon_number,
mean_photon,
fock_probability,
squeezing,
calculate_fidelity,
Expand All @@ -16,4 +16,5 @@
apply_channel,
apply_gate_einsum,
tensor,
fock_operator,
)
13 changes: 10 additions & 3 deletions quasi/_math/fock/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from itertools import chain, product

import numpy as np
from numba import njit
from numba import njit, prange
from scipy.linalg import expm as matrixExp
from scipy.special import factorial
from quasi._math.states import FockState
Expand Down Expand Up @@ -108,6 +108,13 @@ def adagger(cutoff):
return np.conjugate(a(cutoff)).T


@njit
def fock_operator(n, cut):
op = adagger(cut)
for _ in prange(n):
op = op @ op
return op

@njit
def beamsplitter(theta, phi, cutoff, dtype=np.complex128):
sqrt_values = np.sqrt(np.arange(cutoff, dtype=dtype))
Expand Down Expand Up @@ -438,7 +445,7 @@ def homodyne(state, phi, mode, hbar):
def calculate_trace(state):
eqn_indices = [[indices[idx]] * 2 for idx in range(state._num_modes)]
eqn = "".join(chain.from_iterable(eqn_indices))
return np.einsum(eqn, state).real
return np.einsum(eqn, state.dm()).real


def partial_trace(state, n, modes):
Expand Down Expand Up @@ -518,7 +525,7 @@ def apply_channel(state: FockState, kraus_ops, modes):

def norm(state: FockState):
"""returns the norm of the state"""
return calculate_trace(state.dm())
return calculate_trace(state)


def tensor(u, v, n, pos=None):
Expand Down
Empty file added quasi/_math/spin/__init__.py
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions quasi/_math/spin/qutip_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from qutip import sigmax, sigmay, sigmaz, destroy, create, mesolve


class Operators:
raise NotImplementedError


class SpinHamiltonian:
raise NotImplementedError
27 changes: 27 additions & 0 deletions quasi/examples/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from quasi.simulation import Simulation
from quasi.experiement import Experiement
from quasi.components.gates import Squeezing, Beamsplitter, Phase
from math import pi
import numpy as np
from quasi._math.fock import mean_photon
from quasi._math.fock.ops import fock_state, tensor, vacuumStateMixed, adagger, apply_gate_BLAS, fock_operator


squeezing_gate = Squeezing(r=0.1, phi=pi, cutoff=3)

bs_gate = Beamsplitter(theta=pi/4, phi=pi/2, cutoff=3)

phase_gate = Phase(phi=pi/4, cutoff=10)

exp_1 = Experiement(num_modes=3, cutoff=3)

exp_1.state_init(1, modes=[0])

#exp_1.add_operation(bs_gate.get_operator().transpose((0, 2, 1, 3)), [0, 1])
#exp_1.add_operation(squeezing_gate.get_operator(), [0])

exp_1.execute()

state = exp_1.state
print(state.dm().shape)

1 change: 0 additions & 1 deletion quasi/experiement/__init__.py

This file was deleted.

1 change: 1 addition & 0 deletions quasi/experiment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .experiment_manager import Experiment
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np


class Experiement:
class Experiment:
"""Singleton object"""

def __init__(self, num_modes, hbar=2, cutoff=10):
Expand All @@ -31,7 +31,7 @@ def prepare_experiment(self):
cutoff_dim=self.cutoff,
hbar=self.hbar,
)
return self.state
print(f"state {self.state}")

def prepare_multimode(self, data, modes):
r"""
Expand Down Expand Up @@ -70,10 +70,11 @@ def prepare_multimode(self, data, modes):
def alloc(self, n=1):
"""allocate a number of modes at the end of the state."""
# base_shape = [self._trunc for i in range(n)]
print(n)

vac = ops.vacuumStateMixed(n, self.cutoff)

self.data = ops.tensor(self.state.dm(), vac, self.num_modes)
self.data = ops.tensor(self.state.dm(), vac, n)
self.state = FockState(
state_data=self.data,
num_modes=self.num_modes,
Expand All @@ -93,34 +94,46 @@ def _state_init(self, state_preparation: int, modes):
cutoff_dim=self.cutoff,
hbar=self.hbar,
)
# self.prepare_multimode(np.outer(vector, vector.conjugate()), modes)

self.alloc()
# self.prepare_multimode(np.outer(vector, vector.conjugate()), modes)

def execute(self):
self.prepare_experiment()
print(len(self.state_preparations))
if len(self.state_preparations) > 0:
for photon_number, modes in self.state_preparations:
self._state_init(photon_number, modes)
else:

self.prepare_experiment()
operator = ops.fock_operator(photon_number, self.cutoff)

if len(self.channels) > 0:
for channel, modes in self.channels:
self.data = ops.apply_channel(
self.state, kraus_ops=channel, modes=modes
new_st = ops.apply_gate_BLAS(
operator, self.state.dm(), modes, self.num_modes, self.cutoff
)

new_st = new_st / ops.calculate_trace(self.state)

self.state = FockState(
state_data=self.data,
state_data=new_st,
num_modes=self.num_modes,
cutoff_dim=self.cutoff,
)

if len(self.operations) > 0:

for operator, modes in self.operations:
self.data = ops.apply_gate_BLAS(
new_st = ops.apply_gate_BLAS(
operator, self.state.dm(), modes, self.num_modes, self.cutoff
)
new_st = new_st / ops.calculate_trace(self.state)

self.state = FockState(
state_data=new_st,
num_modes=self.num_modes,
cutoff_dim=self.cutoff,
)
if len(self.channels) > 0:
for channel, modes in self.channels:
self.data = ops.apply_channel(
self.state, kraus_ops=channel, modes=modes
)
self.state = FockState(
state_data=self.data,
num_modes=self.num_modes,
Expand Down

0 comments on commit f70cf2e

Please sign in to comment.