Fix #54: derive Gate factories and fromArray from a generic make() constructor - #111
Open
corgab wants to merge 2 commits into
Open
Fix #54: derive Gate factories and fromArray from a generic make() constructor#111corgab wants to merge 2 commits into
corgab wants to merge 2 commits into
Conversation
… make() constructor Gate::make(GateType, qubits, angles) lays parameters out from GateType::shape(), the same metadata fromArray() already dispatched on; every named factory is now a one-line wrapper around it, and fromArray() builds through it too. CircuitBuilder::gate() exposes the same generic entry point as fluent sugar for building a gate from data. A new InvalidCircuitException::gateArity() reports a wrong qubit/angle count.
Angle's degree factory is deg(), not degrees(); the new make() test called a method that doesn't exist, failing CI with a fatal error rather than a test assertion. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E8zT5sUCTC8TgcpsME4WP5
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.
Summary
Gate's 28 named factories each hand-typed the parameter layout thatGateType::shape()already describes generically, andfromArray()re-derived that same shape independently. Adding, renaming, or reshaping a gate touched both. This PR collapses them onto one generic constructor, as the issue proposes.Plan
Gate::make(GateType $type, array $qubits, array $angles = []), which lays qubit indices ontoshape()->qubitKeys()and angles ontoshape()->angleKeys(), in wire order — the same mappingfromArray()used to do inline. AMeasuretype delegates tomeasure(); angles on a measurement are rejected.make(), and havefromArray()build through it too, instead of assembling$paramsby hand.CircuitBuilder::gate(GateType $type, array $qubits, array $angles = [])as the fluent counterpart, for building a gate from data (e.g. replaying a stored circuit) without going through a named method.InvalidCircuitException::gateArity()for a qubit/angle count that doesn't match the gate's shape, since that failure mode didn't exist before (a hand-written factory couldn't be called with the wrong argument count — PHP's own type checker caught that).Changes
src/Circuit/Gate.php:make()added; all 28 factories (h()throughzz()) reduced toreturn self::make(GateType::X, [...qubits], [...angles]);;fromArray()collects qubits/angles into arrays and callsmake()instead of building$paramsinline.src/Circuit/CircuitBuilder.php:gate()added, delegating topush(Gate::make(...)).src/Exceptions/InvalidCircuitException.php:gateArity(string $type, string $kind, int $expected, int $given).README.md: "Adding a Gate" now namesGate::make()/CircuitBuilder::gate()as the one generic constructor the named factories wrap, and mentions->gate()as the entry point for data-driven circuits.Gate::make()is exercised for every non-measureGateTypeagainst its named factory (sametoArray(), same param key order), plusAnglenormalization, wrong qubit/angle counts, and the measure/measure-all/angle-rejection cases.CircuitBuilder::gate()gets the same parity check against its named method, plus a qubit-range validation test. The existing completeness test (everyGatefactory has a matchingGateTypecase) now excludesmake()itself alongsidefromArray().Tests
vendor/bin/pint --testpasses.vendor/bin/pest --compact: 853 tests, 1 skipped (791 before: +62, mostly the per-GateTypedataset tests).Closes #54
🤖 Generated with Claude Code
https://claude.ai/code/session_01E8zT5sUCTC8TgcpsME4WP5
Generated by Claude Code