Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

PauliSumCollector is not compatible with engine

Open MichaelBroughton opened this issue 4 years ago • 9 comments

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 ?

MichaelBroughton avatar Jan 14 '20 01:01 MichaelBroughton

xref #2781

mpharrigan avatar Mar 25 '20 17:03 mpharrigan

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:

  1. 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
  2. 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.
  3. 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.
  4. We could make the serialization error message better and suggest decomposition
  5. 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 ?

balopat avatar Sep 17 '20 20:09 balopat

Would be good to arrive at a decision before 1.0 in case there are breaking changes necessary.

verult avatar Mar 28 '22 20:03 verult

I don't know how well-used PauliSumCollector is

mpharrigan avatar Mar 28 '22 20:03 mpharrigan

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.

dstrain115 avatar May 16 '22 23:05 dstrain115

@verult Do we want to decide on what to do for the Clifford gates as part of the Google gateset refactor?

dstrain115 avatar May 25 '22 00:05 dstrain115

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.

verult avatar Jun 11 '22 00:06 verult

@NoureldinYosri Is this issue now fixed and/or obsolete?

dstrain115 avatar Feb 07 '24 15:02 dstrain115

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

NoureldinYosri avatar Feb 07 '24 22:02 NoureldinYosri