Skip to content

Parameterized DensePauliString (a gate) cannot be applied on qubits to produce an operation #4508

@tanujkhattar

Description

@tanujkhattar
In [2]: import cirq
   ...: import sympy
   ...: gate = sympy.Symbol('t') * cirq.DensePauliString('XYZ')
   ...: gate.on(*cirq.LineQubit.range(3)) # This fails
Click to view the output
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-f86effffd2d5> in <module>
      2 import sympy
      3 gate = sympy.Symbol('t') * cirq.DensePauliString('XYZ')
----> 4 gate.on(*cirq.LineQubit.range(3))

~/quantum/Cirq/cirq-core/cirq/ops/dense_pauli_string.py in on(self, *qubits)
    282
    283     def on(self, *qubits) -> 'cirq.PauliString':
--> 284         return self.sparse(qubits)
    285
    286     # TODO(#3388) Add documentation for Raises.

~/quantum/Cirq/cirq-core/cirq/ops/dense_pauli_string.py in sparse(self, qubits)
    305             raise ValueError('Wrong number of qubits.')
    306
--> 307         return pauli_string.PauliString(
    308             coefficient=self.coefficient,
    309             qubit_pauli_map={q: PAULI_GATES[p] for q, p in zip(qubits, self.pauli_mask) if p},

~/quantum/Cirq/cirq-core/cirq/ops/pauli_string.py in __init__(self, qubit_pauli_map, coefficient, *contents)
    158
    159         self._qubit_pauli_map: Dict[TKey, 'cirq.Pauli'] = qubit_pauli_map or {}
--> 160         self._coefficient = complex(coefficient)
    161         if contents:
    162             m = self.mutable_copy().inplace_left_multiply_by(contents).frozen()

~/opt/anaconda3/envs/cirq/lib/python3.8/site-packages/sympy/core/expr.py in __complex__(self)
    330         result = self.evalf()
    331         re, im = result.as_real_imag()
--> 332         return complex(float(re), float(im))
    333
    334     def _cmp(self, other, op, cls):

~/opt/anaconda3/envs/cirq/lib/python3.8/site-packages/sympy/core/expr.py in __float__(self)
    325         if result.is_number and result.as_real_imag()[1]:
    326             raise TypeError("can't convert complex to float")
--> 327         raise TypeError("can't convert expression to float")
    328
    329     def __complex__(self):

TypeError: can't convert expression to float

This is inconsistent and should be fixed.

This occurs because PauliString does not support symbols but DensePauliString does. Ideally, both of them should have identical behavior and we should discuss how to make it happen.

Note that only the coefficient of DensePauliString can be a symbol, and hence to fix the inconsistency, we'd need to support symbols in coefficient of PauliString.

cc #3242

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/pauliskind/bug-reportSomething doesn't seem to work.kind/design-issueA conversation around designtriage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions