`TwoQubitGateTabulation` returns an `actual_gate` inconsistent with its `local_unitaries` up to a global phase
Describe the issue
TwoQubitGateTabulation.compile_two_qubit_gate returns an actual_gate that differs by a global phase from the product reconstructed from its local_unitaries and base gate. The result documentation defines actual_gate as that product, so the two representations should agree.
This is a return-value consistency issue, not a claim that success=True requires preservation of the target's global phase. The difference does not affect ordinary circuit execution, but can become observable if the compiled block is subsequently placed under coherent quantum control.
Explain how to reproduce the bug or problem
import cirq
import numpy as np
base = cirq.unitary(cirq.CZ)
tabulation = cirq.two_qubit_gate_product_tabulation(base, 0.1, random_state=42)
target = np.exp(1j * np.pi / 4) * base
result = tabulation.compile_two_qubit_gate(target)
built = np.kron(*result.local_unitaries[0])
for pair in result.local_unitaries[1:]:
built = np.kron(*pair) @ base @ built
print("success:", result.success)
print("built matches actual_gate:", np.allclose(built, result.actual_gate))
print("built matches target up to phase:",
cirq.linalg.allclose_up_to_global_phase(built, target))
# Illustrate the phase difference using coherent control and interference.
q0, q1, q2 = cirq.LineQubit.range(3)
for label, block in [("target", target), ("built", built)]:
circuit = cirq.Circuit(
cirq.X(q1), cirq.X(q2), cirq.H(q0),
cirq.MatrixGate(block).controlled().on(q0, q1, q2),
cirq.H(q0),
)
state = cirq.Simulator(dtype=np.complex128).simulate(
circuit, qubit_order=[q0, q1, q2]
).final_state_vector
print(f"{label} P(control=0): {np.sum(np.abs(state[:4])**2):.6f}")
Observed output:
success: True
built matches actual_gate: False
built matches target up to phase: True
target P(control=0): 0.146447
built P(control=0): 0.500000
The coherent-control example illustrates a consequence of reusing the compiled block; it does not demonstrate a failure in Cirq's standard compilation pipelines.
Tell us the version of Cirq where this happens
Cirq 1.7.0, Python 3.11.4, Linux.
Root cause
In _outer_locals_for_unitary, the reconstructed matrix is multiplied by np.conj(target_decomp.global_phase), but the returned local gates are unchanged. Keep actual_gate consistent with their product, or explicitly document the differing phase conventions.
Is this open for working? Can I work on this?
I’d like to work on this issue. I’ll investigate the global-phase handling in TwoQubitGateTabulation and work on a fix with a regression test for the inconsistent actual_gate.
The issue is marked for discussion. We should first decide how to handle it. I would also prefer to assign to the reporter if they are available to take this on.
Cynq meeting - this can be addressed by documentation update that the global phase may not be preserved.
@pavoljuhas Thanks for the clarification. I’m still interested in working on this if the reporter is not available to take it on.
If needed, I’d be happy to update the documentation to clearly state the global-phase convention for actual_gate and add or update a regression test where appropriate.
Thanks for the clarification. I may not have enough time to work through all of the issues I’ve reported, so I think it can be assigned to another contributor who is interested in taking it on.
@pavoljuhas Since the reporter is okay with another contributor taking this on, I’d be very interested in working on it.
I’ve gone through the discussion and understand that the agreed direction is a documentation fix...and not changing the compilation behavior. I can update the relevant TwoQubitGateTabulation / result documentation to make the global-phase convention explicit, and I’m also happy to add or adjust a regression test if that would be useful to ensure the documented behavior remains clear.
I’m available to start on this now. If you’re okay with it, could you please assign the issue to me?
Hi all, thank you for your interest. After looking at the report again, it is actually desirable to fix the inconsistency between the actual_gate and its documented formula. This can be done by preserving the phase of the wanted unitary, provided it is just a phase adjusted base matrix of the associated TwoQubitGateTabulation. The fix in #8403 resolves the initial example above.
Hi all, thank you for your interest. After looking at the report again, it is actually desirable to fix the inconsistency between the actual_gate and its documented formula. This can be done by preserving the phase of the wanted unitary, provided it is just a phase adjusted base matrix of the associated TwoQubitGateTabulation. The fix in #8403 resolves the initial example above.
@pavoljuhas Thanks for the clarification! I came across this issue and had asked if I could work on it, so I've been looking into a fix. I'll go through #8403 to understand the approach. Would you prefer that I adapt my PR based on it, or is the existing fix sufficient?
Would you prefer that I adapt my PR based on it, or is the existing fix sufficient?
PR #8403 is sufficient. After studying the problem, creating a fixup PR was an easy part. We are getting inundated with first time contributors who proxy to AI tools without due understanding and care for what they "produce". It often takes an unjustifiable effort to get such PRs to an acceptable shape. Sorry if that worry was misplaced.