Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coefficient type for GlobalPhase, and convert from cirq correctly #901

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion qualtran/cirq_interop/_cirq_to_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def _cirq_gate_to_bloq(gate: cirq.Gate) -> Bloq:
exponent=gate.exponent, global_shift=gate.global_shift
)

if isinstance(gate, cirq.GlobalPhaseGate) and isinstance(gate.coefficient, (float, complex)):
if isinstance(gate, cirq.GlobalPhaseGate):
return GlobalPhase(coefficient=gate.coefficient)

# No known basic gate, wrap the cirq gate in a CirqGateAsBloq wrapper.
Expand Down
8 changes: 8 additions & 0 deletions qualtran/resource_counting/symbolic_counting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
SymbolicInt = Union[int, sympy.Expr]
document(SymbolicFloat, """A floating point value or a sympy expression.""")

SymbolicComplex = Union[complex, sympy.Expr]
document(SymbolicComplex, """A complex value or a sympy expression.""")


@frozen
class Shaped:
Expand Down Expand Up @@ -118,6 +121,11 @@ def acos(x: SymbolicFloat) -> SymbolicFloat:
return sympy.acos(x)


def sconj(x: SymbolicComplex) -> SymbolicComplex:
"""Compute the complex conjugate."""
return sympy.conjugate(x) if is_symbolic(x) else np.conjugate(x)


def slen(x: Union[Sized, Shaped]) -> SymbolicInt:
if isinstance(x, Shaped):
return x.shape[0]
Expand Down
Loading