Skip to content

Commit 480f863

Browse files
authored
Fix coefficient type for GlobalPhase, and convert from cirq correctly (#901)
* Fix coefficient type `GlobalPhase`, and convert from cirq correctly * add `sconj` to symbolic utils
1 parent 44a86a8 commit 480f863

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

qualtran/bloqs/basic_gates/global_phase.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
if TYPE_CHECKING:
2626
from qualtran import CompositeBloq
27+
from qualtran.resource_counting.symbolic_counting_utils import SymbolicComplex
2728

2829

2930
@frozen
@@ -35,7 +36,7 @@ class GlobalPhase(CirqGateAsBloqBase):
3536
eps: precision
3637
"""
3738

38-
coefficient: complex
39+
coefficient: 'SymbolicComplex'
3940
eps: float = 1e-11
4041

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

4849
def adjoint(self) -> 'GlobalPhase':
49-
return attrs.evolve(self, coefficient=complex(self.coefficient).conjugate())
50+
from qualtran.resource_counting.symbolic_counting_utils import sconj
51+
52+
return attrs.evolve(self, coefficient=sconj(self.coefficient))
5053

5154
def pretty_name(self) -> str:
5255
return 'GPhase'

qualtran/cirq_interop/_cirq_to_bloq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def _cirq_gate_to_bloq(gate: cirq.Gate) -> Bloq:
399399
exponent=gate.exponent, global_shift=gate.global_shift
400400
)
401401

402-
if isinstance(gate, cirq.GlobalPhaseGate) and isinstance(gate.coefficient, (float, complex)):
402+
if isinstance(gate, cirq.GlobalPhaseGate):
403403
return GlobalPhase(coefficient=gate.coefficient)
404404

405405
# No known basic gate, wrap the cirq gate in a CirqGateAsBloq wrapper.

qualtran/resource_counting/symbolic_counting_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
SymbolicInt = Union[int, sympy.Expr]
2525
document(SymbolicFloat, """A floating point value or a sympy expression.""")
2626

27+
SymbolicComplex = Union[complex, sympy.Expr]
28+
document(SymbolicComplex, """A complex value or a sympy expression.""")
29+
2730

2831
@frozen
2932
class Shaped:
@@ -118,6 +121,11 @@ def acos(x: SymbolicFloat) -> SymbolicFloat:
118121
return sympy.acos(x)
119122

120123

124+
def sconj(x: SymbolicComplex) -> SymbolicComplex:
125+
"""Compute the complex conjugate."""
126+
return sympy.conjugate(x) if is_symbolic(x) else np.conjugate(x)
127+
128+
121129
def slen(x: Union[Sized, Shaped]) -> SymbolicInt:
122130
if isinstance(x, Shaped):
123131
return x.shape[0]

0 commit comments

Comments
 (0)