Skip to content

Commit

Permalink
perf(fock): Caching subspaces
Browse files Browse the repository at this point in the history
Calculating the subspaces (the indices, essentially) is a costly calculation,
and it is beneficial to have them stored in a cache for later purposes.
  • Loading branch information
Kolarovszki committed Jan 31, 2024
1 parent 91bb615 commit 1680979
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
9 changes: 2 additions & 7 deletions piquasso/_backends/fock/pure/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,7 @@ def passive_linear(
state._calculator, state._config
).astype(np.complex128)

subspace = FockSpace(
d=len(interferometer),
cutoff=state._space.cutoff,
calculator=calculator,
config=state._config,
)
subspace = state._get_subspace(dim=len(interferometer))

subspace_transformations = _get_interferometer_on_fock_space(
interferometer, subspace, calculator
Expand Down Expand Up @@ -553,7 +548,7 @@ def _apply_active_gate_matrix_to_state(
calculator = state._calculator
state_vector = state._state_vector
space = state._space
auxiliary_subspace = state._auxiliary_subspace
auxiliary_subspace = state._get_subspace(state.d - 1)

@calculator.custom_gradient
def _apply_active_gate_matrix(state_vector, matrix):
Expand Down
41 changes: 30 additions & 11 deletions piquasso/_backends/fock/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,40 @@ def __init__(
) -> None:
super().__init__(calculator=calculator, config=config)

self._space = fock.FockSpace(
d=d,
cutoff=self._config.cutoff,
calculator=calculator,
config=self._config,
)
# NOTE: This is instantiated here, since it is costly to do so, and is needed
# for several, repeating calculations.
self._auxiliary_subspace = fock.FockSpace(
d=d - 1,
self._initialize_subspaces(d)

def _initialize_subspaces(self, d):
"""
NOTE: Calculating the subspaces (the indices, essentially) is a costly
calculation, and it is beneficial to have them stored for later purposes
"""
self._space = self._init_subspace(d)

self._subspace_cache = {
1: self._init_subspace(1),
2: self._init_subspace(2),
d - 1: self._init_subspace(d - 1),
d: self._space,
}

def _init_subspace(self, dim):
return fock.FockSpace(
d=dim,
cutoff=self._config.cutoff,
calculator=calculator,
calculator=self._calculator,
config=self._config,
)

def _get_subspace(self, dim):
if dim in self._subspace_cache.keys():
return self._subspace_cache[dim]

subspace = self._init_subspace(dim)

Check warning on line 62 in piquasso/_backends/fock/state.py

View check run for this annotation

Codecov / codecov/patch

piquasso/_backends/fock/state.py#L62

Added line #L62 was not covered by tests

self._subspace_cache[dim] = subspace

Check warning on line 64 in piquasso/_backends/fock/state.py

View check run for this annotation

Codecov / codecov/patch

piquasso/_backends/fock/state.py#L64

Added line #L64 was not covered by tests

return subspace

Check warning on line 66 in piquasso/_backends/fock/state.py

View check run for this annotation

Codecov / codecov/patch

piquasso/_backends/fock/state.py#L66

Added line #L66 was not covered by tests

@property
def d(self) -> int:
return self._space.d
Expand Down

0 comments on commit 1680979

Please sign in to comment.