Add the Phase(gamma) unitary with exact naming, structural control, and hardware lowering - #672
Open
ciaranra wants to merge 9 commits into
Open
Add the Phase(gamma) unitary with exact naming, structural control, and hardware lowering#672ciaranra wants to merge 9 commits into
ciaranra wants to merge 9 commits into
Conversation
…ck without float_cmp
…nd hardware lowering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.