Cirq
Cirq copied to clipboard
PauliSumCollector is not compatible with engine
In PauliSum Collector when doing the basis transformations with
https://github.com/quantumlib/Cirq/blob/3469e5c9bf99b4f54fe147379ff58e1642754dc0/cirq/work/pauli_sum_collector.py#L112
The gates that get returned are cirq.SingleQubitClifford(...)
which won't play nice with the serializers. A quick fix is to just add a cirq.decompose
around the result of pauli_string.to_z_basis_ops()
, but I wasn't sure if people might have other opinions ?
xref #2781
Just to clarify this is what you are facing, right?
cirq.google.SYC_GATESET.serialize(cirq.Circuit(cirq.SingleQubitCliffordGate.Y(cirq.NamedQubit("a"))))
Throws an error
ValueError: Cannot serialize op cirq.SingleQubitCliffordGate(X:-X, Y:+Y, Z:-Z).on(cirq.NamedQubit('a')) of type <class 'cirq.ops.clifford_gate.SingleQubitCliffordGate'>
While
cirq.google.SYC_GATESET.serialize(cirq.Circuit(cirq.decompose(cirq.SingleQubitCliffordGate.Y(cirq.NamedQubit("a")))))
does the trick.
To answer your question: cirq.decompose happens to compile to the gateset that is compatible with the SYC_GATESET serializers, so today that seems to be your best bet.
Thoughts:
- there is no nicer option than
cirq.decompose()
to convert a SingleQubitCliffordGate in general to regular Pauli gates that are recognized by our common serializers - we could change
to_z_basis_ops
so it returns a sequence of regular Pauli gate (using cirq.decompose) - the problem with that is that some algorithms might like the fact that it returns the SingleQubitCliffordGate representation. - we could add decomposition in the serializers as an automated fallback strategy, guarded by a parameter - eg.
SYC_GATESET.serialize(circuit, decompose_on_fail=True)
would try to decompose an unrecognized gate. - We could make the serialization error message better and suggest decomposition
- We could add a SingleQubitCliffordGate serializer to our gatesets that would do the decomposition.
I think 3 and 4 would be my preference here. @viathor @dstrain115 @dabacon ?
Would be good to arrive at a decision before 1.0 in case there are breaking changes necessary.
I don't know how well-used PauliSumCollector
is
We should just be able to silently convert these into PhasedXZGates if necessary though. Would that satisfy this bug?
It looks like that is option 5 that Balint suggested above. I don't think option 3 is a good idea, since that could mess with moments during serialization, which would not be expected.
@verult Do we want to decide on what to do for the Clifford gates as part of the Google gateset refactor?
In the device refactor, the principle we've followed for arbitrary single qubit gates (e.g. cirq.H
) is to not accept them directly and instead have them go through a transformer, so that parameters of PhasedXZGate, a device native gate, are clear to the user, and the circuit becomes serializable:
device = cirq_google.get_engine().get_processor('processor_name').get_device()
circuit = cirq.Circuit(cirq.SingleQubitCliffordGate.Y(cirq.NamedQubit("a")))
cirq.optimize_for_target_gateset(circuit, gateset=device.metadata.compilation_target_gatesets[0])
So I'm leaning towards leaving it as is.
@NoureldinYosri Is this issue now fixed and/or obsolete?
SingleQubitCliffordGate
is now serializable (but they converted to PhasedXZ gates in the process), however when interacting with the engine before serialization some validation happens and one of the steps checks if the operation is in the gateset supported by the device. no gateset explictly supports SingleQubitCliffordGate. for now running cirq.optimize_for_target_gateset(circuit, gateset=device.metadata.compilation_target_gatesets[0])
as suggest by https://github.com/quantumlib/Cirq/issues/2677#issuecomment-1152818978 is the solution.
unless we want to add SingleQubitCliffordGate to the supported gatesets, should we?
for reference here are the supported gatesets https://github.com/quantumlib/Cirq/blob/a3eed6b97490556cf1dda82928a0aa9ea8798da1/cirq-google/cirq_google/devices/grid_device.py#L56-L64