Add the Phase(gamma) unitary with exact naming, structural control, and hardware lowering - #672
Add the Phase(gamma) unitary with exact naming, structural control, and hardware lowering#672ciaranra wants to merge 9 commits into
Conversation
…ck without float_cmp
…nd hardware lowering
|
Reviewed the parts that survive a rebase. I have not reviewed the diff itself, for the reason in the last section. The
|
|
I attempted the The refresh is blocked on a phase-convention collision, not on mechanics44 commits behind, 12 conflicted files. Most are mechanical: this branch adds a Two conflicts are not mechanical:
A correction to something I said earlierIn my previous comment I described the broadened predicate as The design point:
|
|
Refreshed against Decision 1: dev's phase conventions win
Decision 2: one-qubit
|
|
Applied the narrowing on What changed and whyA phase is a mathematical scalar. The two-operand limit is a property of the hardware we lower to, not of the operator: So:
I sealed this variant in the previous commit on the argument that #701 had to seal What deliberately did not change
Verification, run independently
Tests that pinned the removed rule were replaced rather than deleted: the arity-refusal cases now assert that larger arities are accepted and produce the expected larger diagonals, and Still open
|
Stacked on #668 (the branch includes its commits; merge #668 first).
What
One new unitary,
Phase(gamma), with one rule: on an operand setSof zero, one, or two qubits it multiplies the amplitude byexp(i gamma)on the subspace where every qubit inSis|1>and leaves everything else alone.S = {}is a global phase (the scalarexp(i gamma)).S = {q}isdiag(1, exp(i gamma)), exactlyU(0, 0, gamma).S = {c, t}isdiag(1, 1, 1, exp(i gamma)).|S| > 2is refused at construction.PhaseonSby a qubitcis structurallyPhaseonS + {c}.This is layers 1 and 2 of the Phase(gamma) design (representation and hardware lowering). No
GateType, byte-level command, simulator fast path, or ingress rewiring yet; those follow once the benchmark decides the command shape.API (pecos-core)
Unitary::Phase { gamma: Angle64, num_qubits: u8 }andUnitaryRep::phase_gate(gamma, qubits).UnitaryRep::control(self, c) -> Result<UnitaryRep, ControlError>: structural forPhase; refuses a control already inS, a third operand, a non-Phaserep, and a descriptor whose operand count disagrees with its qubit list.controlled_rotations::lower_phase(gamma_radians, &[QubitId]) -> Vec<Gate>:{}lowers to no gates (hardware cannot see a scalar; the caller keeps it),{q}toU(0, 0, gamma),{c, t}throughlower_cphase.is_cliffordreads the shared exact Clifford-angle table (try_simplify_rotation): one qubit at multiples ofpi/2, two qubits at0andpi.to_named_gatereturns a name only when the matrices are exactly equal:I, SZ, Z, SZdg, T, Tdgon one qubit,CZon two. Adjoint,is_hermitian,to_pauli_string,is_pauli_equivalent/try_to_pauli,Unitary::is_pauli/try_to_pauli,to_clifford_rep, the hardware decomposition, thephase()accessor (a zero-operandPhaseis the scalar itself), and circuit-diagram rendering handlePhase.Fix found on the way
lower_cphasehalved the sourcef64beforeAngle64reduction, so atlambda = 2pi (mod 4pi)theRZZ(-pi)andRZ(+pi)legs both storedHALF_TURNand the lowering executed as-Iinstead ofI(QASM/PHIRcp(2pi)ondev).CPhaseis 2π-periodic, so the angle is now reduced to(-pi, pi]first; every halved angle then lies in(-pi/2, pi/2]and the lowering is exact for every input.CRZ/CRX/CRYare 4π-periodic and cannot use that reduction; their documented ±1 attheta = 2pi (mod 4pi)is intrinsic to the two-gate form and is filed as #670 (the exact fix is a zero-operandPhasefrom the ingress, which this PR makes possible).Tests
crates/pecos-simulators/tests/phase_gate_contract.rsis the specification: the matrix follows the rule at nine angles including±2pi,pi,3pi/2, forS = {},{0},{1},{0,1},{1,0}; single-qubitPhaseequalsU(0,0,gamma)entrywise; the lowering composed in the dense path and executed onStateVecSoAequals the rule entrywise (1e-12);control()composes and refuses as specified; Clifford membership and exact naming follow the angle table. Unit tests cover thecontrol()error variants,lower_phaseon{}, the T/Tdg names, the Pauli views, and the zero-operandphase().Follow-ups filed
to_named_gatenames rotations up to global phase (RZ(pi) -> Z) whilePhasenames exactly; split or document.theta = 2pi (mod 4pi).Verification
cargo fmt --all -- --check;cargo clippy --workspace --all-targets -- -D warnings(default and--all-featureslanes);pecos rust test --profile debug(full recipe);just lint check. Mutation: removing thelower_cphasereduction fails both lowering tests in the contract. Two independent reviews (fresh-context and cross-model) ran on the diff; every finding is fixed or refuted in this description.