Skip to content

Commit

Permalink
Merge pull request #62 from zapatacomputing/zqs-1188-accelerate-qulac…
Browse files Browse the repository at this point in the history
…s-integratio

feat(wavefunction): cache ordering results
  • Loading branch information
Athena Caesura authored Sep 28, 2022
2 parents 8b9ed8a + 7784fa9 commit 64b52db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/orquestra/quantum/operators/_pauli_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ def __repr__(self):
return str(zero_identity_term)
return " + ".join([str(term) for term in self.terms])

def __hash__(self):
return hash(tuple(self.terms))

@property
def is_constant(self) -> bool:
return len(self.terms) == 0 or all([term.is_constant for term in self.terms])
Expand Down
11 changes: 8 additions & 3 deletions src/orquestra/quantum/wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# © Copyright 2021-2022 Zapata Computing Inc.
################################################################################
import json
from functools import lru_cache
from math import log2
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union
from warnings import warn
Expand Down Expand Up @@ -219,12 +220,16 @@ def flip_wavefunction(wavefunction: Wavefunction):


def flip_amplitudes(amplitudes: Union[Sequence[complex], np.ndarray]) -> np.ndarray:
number_of_states = len(amplitudes)
ordering = [
ordering = _get_ordering(len(amplitudes))
return np.array([amplitudes[i] for i in ordering])


@lru_cache()
def _get_ordering(number_of_states: int) -> List[int]:
return [
_flip_bits(n, number_of_states.bit_length() - 1)
for n in range(number_of_states)
]
return np.array([amplitudes[i] for i in ordering])


def _flip_bits(n, num_bits):
Expand Down

0 comments on commit 64b52db

Please sign in to comment.