You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multi-qubit X gate promoted to TOFFOLI gate and could not be serialized by TFQ. #480
When a multi-qubit X gate is created by the .controlled method, it maybe automatically promoted to cirq.TOFFOLI gate which isn't supported by TFQ.
Minimal example:
importtensorflow_quantumastfqqbs=cirq.GridQubit.rect(4, 1)
circuit=cirq.Circuit(
cirq.X.controlled(num_controls=3).on(*qbs)
)
try:
layer([circuit])
exceptExceptionase:
print(f"Exception:\n{e}")
# Pinrt:# Exception:# Cannot serialize op cirq.TOFFOLI(cirq.GridQubit(1, 0), cirq.GridQubit(2, 0), cirq.GridQubit(3, 0)) of type <class 'cirq.ops.three_qubit_gates.CCXPowGate'>
Currently, the work-around is to use cirq.ControlledGate(sub_gate=cirq.X, num_controls=3).on(*qbs) instead.
Similar problem happen to cirq.Z as well, which is promoted to cirq.CCZ.
Luckily, cirq.Y works because cirq.CCY does not exist.
After talking this over with the Cirq people it turns out that this is intended behavior. So we have to turn around and update our serializer functionality to support Toffoli and CCZ.
I am having the same issue, but the workaround @we-taper mentioned does not work for me, since I am getting the same error for the ControlledGate. @we-taper: Are you using the regular TFQ version or the nightly one?
Ok, thank you for the answer, @we-taper :). I was using the regular version, so that might have caused the different behaviour. I meanwhile have found a workaround, though. (I just implemented the Toffoli-gate in a decomposed manner, as described in https://en.wikipedia.org/wiki/Toffoli_gate#/media/File:Qcircuit_ToffolifromCNOT.svg , that worked with the regular TFQ version, too.)
Actually, why not working with the nightly-version? The major benefit is that gates will be implemented natively which means faster execution. So far it has been quite smooth for me to use the nightly version (or to be specific tfq-nightly==0.5.0.dev20210218).
I see there is a pull request under review to fixing this thread, is there plans to merge that soon? It's not breaking since you can use the techniques presented above to solve the problem, but it's a weird interaction.
Activity
MichaelBroughton commentedon Feb 22, 2021
After talking this over with the Cirq people it turns out that this is intended behavior. So we have to turn around and update our serializer functionality to support Toffoli and CCZ.
Danielle-Schuman commentedon Mar 13, 2021
I am having the same issue, but the workaround @we-taper mentioned does not work for me, since I am getting the same error for the ControlledGate. @we-taper: Are you using the regular TFQ version or the nightly one?
we-taper commentedon Mar 14, 2021
@Danielle-Schuman I was using the nightly one at the time.
Danielle-Schuman commentedon Mar 15, 2021
Ok, thank you for the answer, @we-taper :). I was using the regular version, so that might have caused the different behaviour. I meanwhile have found a workaround, though. (I just implemented the Toffoli-gate in a decomposed manner, as described in https://en.wikipedia.org/wiki/Toffoli_gate#/media/File:Qcircuit_ToffolifromCNOT.svg , that worked with the regular TFQ version, too.)
we-taper commentedon Mar 15, 2021
Thanks! @Danielle-Schuman
Actually, why not working with the nightly-version? The major benefit is that gates will be implemented natively which means faster execution. So far it has been quite smooth for me to use the nightly version (or to be specific
tfq-nightly==0.5.0.dev20210218).lockwo commentedon Jul 6, 2021
I see there is a pull request under review to fixing this thread, is there plans to merge that soon? It's not breaking since you can use the techniques presented above to solve the problem, but it's a weird interaction.
Merge pull request tensorflow#480 from quantumlib/cuquantum-install-doc
mhucka commentedon Sep 13, 2025
Closing because this issue seems to have been addressed in the past.