Skip to content

Commit

Permalink
Merge branch 'main' into 2024-04/checks-janitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan authored Apr 29, 2024
2 parents da5bdce + e6d6e01 commit 0c9be3b
Show file tree
Hide file tree
Showing 49 changed files with 679 additions and 178 deletions.
8 changes: 5 additions & 3 deletions dev_tools/qualtran_dev_tools/jupyter_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class NotebookSpecV2:

@directory.default
def _default_directory(self) -> str:
return str(Path(self.module.__file__).parent)
path = self.module.__file__
assert path is not None
return str(Path(path).parent)

@property
def path_stem(self):
Expand Down Expand Up @@ -156,7 +158,7 @@ class _PyCell(_Cell):
cell_id: str


def get_bloq_doc_cells(bloqdoc: BloqDocSpec, cid_prefix: str) -> List[_MarkdownCell]:
def get_bloq_doc_cells(bloqdoc: BloqDocSpec, cid_prefix: str) -> List[_Cell]:
"""Cells introducing the `bloq_cls`"""

md_doc: str = '\n'.join(get_markdown_docstring_lines(bloqdoc.bloq_cls))
Expand Down Expand Up @@ -230,7 +232,7 @@ def get_call_graph_cells(bloqdoc: BloqDocSpec, cid_prefix: str) -> List[_Cell]:


def get_cells(bloqdoc: BloqDocSpec) -> List[_Cell]:
cells = []
cells: List[_Cell] = []
cid_prefix = f'{bloqdoc.bloq_cls.__name__}'
cells += get_bloq_doc_cells(bloqdoc, cid_prefix)
cells += get_example_instances_cells(bloqdoc, cid_prefix)
Expand Down
2 changes: 1 addition & 1 deletion qualtran/_infra/bloq_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def bloq_example(_func: Callable[[], _BloqType], **kwargs: Any) -> BloqExample[_

@typing.overload
def bloq_example(
_func: None, *, generalizer: _GeneralizerType = lambda x: x
_func: None = None, *, generalizer: _GeneralizerType = lambda x: x
) -> Callable[[Callable[[], _BloqType]], BloqExample[_BloqType]]:
...

Expand Down
7 changes: 5 additions & 2 deletions qualtran/bloqs/basic_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

if TYPE_CHECKING:
from qualtran import CompositeBloq
from qualtran.resource_counting.symbolic_counting_utils import SymbolicComplex


@frozen
Expand All @@ -35,7 +36,7 @@ class GlobalPhase(CirqGateAsBloqBase):
eps: precision
"""

coefficient: complex
coefficient: 'SymbolicComplex'
eps: float = 1e-11

@property
Expand All @@ -46,7 +47,9 @@ def decompose_bloq(self) -> 'CompositeBloq':
raise DecomposeTypeError(f"{self} is atomic")

def adjoint(self) -> 'GlobalPhase':
return attrs.evolve(self, coefficient=complex(self.coefficient).conjugate())
from qualtran.resource_counting.symbolic_counting_utils import sconj

return attrs.evolve(self, coefficient=sconj(self.coefficient))

def pretty_name(self) -> str:
return 'GPhase'
Expand Down
9 changes: 5 additions & 4 deletions qualtran/bloqs/chemistry/df/double_factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
QBit,
Register,
Signature,
Soquet,
SoquetT,
)
from qualtran.bloqs.basic_gates import CSwap, Hadamard, Toffoli
Expand Down Expand Up @@ -156,17 +157,17 @@ def short_name(self) -> str:
def build_composite_bloq(
self,
bb: 'BloqBuilder',
succ_l: SoquetT,
succ_l: Soquet,
l_ne_zero: SoquetT,
succ_p: SoquetT,
succ_p: Soquet,
p: SoquetT,
rot_aa: SoquetT,
spin: SoquetT,
xi: SoquetT,
offset: SoquetT,
rot: SoquetT,
rotations: SoquetT,
sys: SoquetT,
sys: NDArray[Soquet], # type: ignore[type-var]
) -> Dict[str, 'SoquetT']:
# 1st half
in_prep = InnerPrepareDoubleFactorization(
Expand Down Expand Up @@ -374,7 +375,7 @@ def signature(self) -> Signature:
def build_composite_bloq(
self,
bb: 'BloqBuilder',
ctrl: SoquetT,
ctrl: NDArray[Soquet], # type: ignore[type-var]
l: SoquetT,
p: SoquetT,
spin: SoquetT,
Expand Down
3 changes: 1 addition & 2 deletions qualtran/bloqs/chemistry/ising/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,4 @@ def get_1d_ising_lcu_coeffs(
spins = cirq.LineQubit.range(n_spins)
ham = get_1d_ising_hamiltonian(spins, j_zz_strength, gamma_x_strength)
coeffs = np.array([term.coefficient.real for term in ham])
lcu_coeffs = coeffs / np.sum(abs(coeffs))
return lcu_coeffs
return coeffs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import numpy as np
from attrs import evolve, field, frozen
from numpy.typing import NDArray

from qualtran import (
Bloq,
Expand Down Expand Up @@ -107,7 +108,7 @@ def short_name(self) -> str:
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
if self.is_adjoint:
# inverting inequality tests at zero Toffoli.
return {}
return set()
else:
return {(Toffoli(), 6 * self.num_bits_t + 2)}

Expand Down Expand Up @@ -147,6 +148,7 @@ def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol':
(c_idx,) = soq.idx
filled = bool(self.cvs[c_idx])
return Circle(filled)
raise ValueError(f'Unknown name: {soq.reg.name}')

def build_composite_bloq(
self, bb: BloqBuilder, ctrl: SoquetT, sel: SoquetT, targets: SoquetT, junk: SoquetT
Expand Down Expand Up @@ -294,9 +296,9 @@ def build_composite_bloq(
r: SoquetT,
s: SoquetT,
mu: SoquetT,
nu_x: SoquetT,
nu_y: SoquetT,
nu_z: SoquetT,
nu_x: Soquet,
nu_y: Soquet,
nu_z: Soquet,
m: SoquetT,
succ_nu: SoquetT,
l: SoquetT,
Expand Down Expand Up @@ -462,7 +464,7 @@ def short_name(self) -> str:
def build_composite_bloq(
self,
bb: BloqBuilder,
ham_ctrl: SoquetT,
ham_ctrl: NDArray[Soquet], # type: ignore[type-var]
i_ne_j: SoquetT,
plus_t: SoquetT,
i: SoquetT,
Expand All @@ -478,7 +480,7 @@ def build_composite_bloq(
m: SoquetT,
l: SoquetT,
sys: SoquetT,
proj: SoquetT,
proj: NDArray[Soquet], # type: ignore[type-var]
) -> Dict[str, 'SoquetT']:
# ancilla for swaps from electronic and projectile system registers.
# we assume these are left in a clean state after SELECT operations
Expand Down Expand Up @@ -543,8 +545,10 @@ def build_composite_bloq(
j, sys, q = bb.add(
MultiplexedCSwap3D(self.num_bits_p, self.eta), sel=j, targets=sys, junk=q
)
_ = [bb.free(pi) for pi in p]
_ = [bb.free(qi) for qi in q]
for pi in p:
bb.free(pi)
for qi in q:
bb.free(qi)
ham_ctrl[:] = flag_t, flag_t_mean, flag_uv, flag_proj
bb.free(rl)
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import numpy as np
from attrs import frozen
from numpy.typing import NDArray

from qualtran import (
Bloq,
Expand All @@ -28,6 +29,7 @@
QBit,
Register,
Signature,
Soquet,
SoquetT,
)
from qualtran.bloqs.basic_gates import Toffoli
Expand Down Expand Up @@ -131,7 +133,7 @@ def signature(self) -> Signature:
@staticmethod
def _reshape_reg(
bb: BloqBuilder, in_reg: SoquetT, out_shape: Tuple[int, ...], bitsize: int
) -> SoquetT:
) -> NDArray[Soquet]: # type: ignore[type-var]
"""Reshape registers allocated as a big register.
Example:
Expand Down Expand Up @@ -162,6 +164,7 @@ def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol':
return TextBox('×(x)')
elif soq.reg.name == 'junk':
return TextBox('×(y)')
raise ValueError(f'Unknown name: {soq.reg.name}')

def short_name(self) -> str:
return 'MultiSwap'
Expand Down Expand Up @@ -279,9 +282,9 @@ def build_composite_bloq(
r: SoquetT,
s: SoquetT,
mu: SoquetT,
nu_x: SoquetT,
nu_y: SoquetT,
nu_z: SoquetT,
nu_x: Soquet,
nu_y: Soquet,
nu_z: Soquet,
m: SoquetT,
succ_nu: SoquetT,
l: SoquetT,
Expand Down Expand Up @@ -445,9 +448,9 @@ def build_composite_bloq(
r: SoquetT,
s: SoquetT,
mu: SoquetT,
nu_x: SoquetT,
nu_y: SoquetT,
nu_z: SoquetT,
nu_x: Soquet,
nu_y: Soquet,
nu_z: Soquet,
m: SoquetT,
l: SoquetT,
sys: SoquetT,
Expand Down Expand Up @@ -490,8 +493,10 @@ def build_composite_bloq(
j, sys, q = bb.add(
MultiplexedCSwap3D(self.num_bits_p, self.eta), sel=j, targets=sys, junk=q
)
_ = [bb.free(pi) for pi in p]
_ = [bb.free(qi) for qi in q]
for pi in p:
bb.free(pi)
for qi in q:
bb.free(qi)
bb.free(rl)
return {
'tuv': tuv,
Expand Down
13 changes: 13 additions & 0 deletions qualtran/bloqs/chemistry/quad_fermion/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading

0 comments on commit 0c9be3b

Please sign in to comment.