Skip to content

Add QuantumGates module#50

Open
lkdvos wants to merge 6 commits into
mainfrom
quantumgates
Open

Add QuantumGates module#50
lkdvos wants to merge 6 commits into
mainfrom
quantumgates

Conversation

@lkdvos

@lkdvos lkdvos commented Jul 3, 2026

Copy link
Copy Markdown
Member

This adds a QuantumGates submodule with the typical quantum-computing gates (a good chunk of the Clifford and non-Clifford ones), returned as TensorMaps on qubit spaces. It has been rebased onto main and rewritten on top of the new @operator macro (#51).

Following the earlier discussion, the boilerplate-heavy first version has been reworked:

  • Rewritten every gate through @operator. Each gate is now defined by a single non-symmetric Trivial reference method, and the element-type/symmetry forwarding methods plus the symmetric version are generated automatically via symmetrize/basis_transform. Because the qubit computational basis already coincides with the U(1) charge eigenbasis, basis_transform is just the identity: number-conserving gates get their U1Irrep version for free, while qubit-flipping gates simply throw an ArgumentError under U1Irrep. This removes all the hand-written per-symmetry boilerplate (e.g. pauli_z's explicit U(1) branch).
  • Dropped the sqrt gates (sqrt_x/SX, sqrt_swap, sqrt_iswap) — these are trivially reconstructible from the primitives (sqrt_iswap(...) = sqrt(iswap(...)), etc.) and don't add much.
  • Added the two-site rotation gates Rxx, Ryy, Rzz, Rzx, i.e. exp(-iθ/2 · P⊗P). Rzz is diagonal and so gains a U1Irrep version; the others are Trivial-only.
  • Rotation angle is now a keyword θ rather than positional, matching the package's spin=/cutoff= convention (and letting the parametric gates go through @operator uniformly). The ASCII name theta is accepted as an alias, so both rotation_x(; θ=π/2) and rotation_x(; theta=π/2) work.

Still open for opinions:

  • The names themselves — the uppercase aliases match what's common in the literature, but don't fit the existing operator naming very well. We could drop the long function names and keep only the aliases.
  • Whether keeping the U1Irrep option is actually worth it.

Implemented gates

Function Alias(es) Sites Supported symmetries
qubit_space Trivial, U1Irrep
pauli_x X 1 Trivial
pauli_y Y 1 Trivial
pauli_z Z 1 Trivial, U1Irrep
proj_0 P0 1 Trivial, U1Irrep
proj_1 P1 1 Trivial, U1Irrep
hadamard H 1 Trivial
s_gate S 1 Trivial, U1Irrep
t_gate T 1 Trivial, U1Irrep
phase_shift P 1 Trivial, U1Irrep
rotation_x Rx 1 Trivial
rotation_y Ry 1 Trivial
rotation_z Rz 1 Trivial, U1Irrep
cnot CNOT, CX 2 Trivial
cy CY 2 Trivial
cz CZ 2 Trivial, U1Irrep
ch CH 2 Trivial
cs CS 2 Trivial, U1Irrep
cphase CP 2 Trivial, U1Irrep
swap SWAP 2 Trivial, U1Irrep
iswap ISWAP 2 Trivial, U1Irrep
dcx DCX 2 Trivial
ecr ECR 2 Trivial
rotation_xx Rxx 2 Trivial
rotation_yy Ryy 2 Trivial
rotation_zz Rzz 2 Trivial, U1Irrep
rotation_zx Rzx 2 Trivial
toffoli TOFFOLI, CCX 3 Trivial
fredkin FREDKIN, CSWAP 3 Trivial, U1Irrep

🤖 Generated with Claude Code

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/TensorKitTensors.jl 100.00% <ø> (ø)
src/quantumgates.jl 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos lkdvos added the documentation Improvements or additions to documentation label Jul 3, 2026
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

After the build completes, the updated documentation will be available here

Comment thread src/quantumgates.jl Outdated
@VictorVanthilt

VictorVanthilt commented Jul 6, 2026

Copy link
Copy Markdown
Member

I feel like the rotation gates are still useful, also just to not have to remember the conventions, but maybe the sqrt ones aren't the most helpful?

Agreed, i would keep Rx(theta) and get rid of sqrt_x, sqrt_swap and sqrt_iswap.

Although the aliases are very much in line with the names often used in literature, exporting things like H and T would interfere with almost all of my scripts ...

@lkdvos

lkdvos commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

I think the exports are probably okay, this is within the submodule, so you would only import them if you included that in its entirety (since we already have name clashes between Hubbard and TJ too). I don't think this can accidentally leak into previous scripts, you really need using TensorKitTensors.QuantumGates

@VictorVanthilt

Copy link
Copy Markdown
Member

That @operator macro + the symmetrize method is a very clean solution!
Forgive me if this is a stupid question, but why would U1Irrep options not be worth it?

@lkdvos

lkdvos commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

I'm not sure if anyone is ever really using U1 for quantum computing operations, it seems like in that context this doesn't happen all that often, which is more or less why I was asking if this is useful

lkdvos and others added 6 commits July 25, 2026 07:36
Introduce a QuantumGates submodule exposing common single-, two-, and
three-qubit gates as TensorMaps on qubit spaces (Trivial and U1Irrep).

Each gate is defined through the @operator macro from a single non-symmetric
reference method: the symmetric terminal routes the dense operator through
`symmetrize` using an identity `basis_transform` (the qubit computational basis
already coincides with the U(1) charge eigenbasis). Number-conserving gates
gain their U(1) version automatically, while gates that flip qubits raise an
ArgumentError under U1Irrep.

Parametric gates (phase_shift, rotation_x/y/z, cphase) take the rotation angle
as a required keyword `θ`, with the ASCII name `theta` accepted as an alias,
matching the package convention of keyword operator parameters.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add the parametric two-qubit rotation gates rotation_xx/Rxx, rotation_yy/Ryy,
rotation_zz/Rzz and rotation_zx/Rzx, i.e. exp(-iθ/2 P⊗P) for P⊗P in
{X⊗X, Y⊗Y, Z⊗Z, Z⊗X}. Like the single-qubit rotations they take the angle as
a required keyword `θ` (with `theta` accepted as an ASCII alias). rotation_zz is
diagonal and gains its U(1) version automatically; the others reject U1Irrep.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- swap: build the identity at the requested eltype (`id(elt, q ⊗ q)`); permuting
  a non-symmetric tensor preserves eltype, so the extra scalar multiplication is
  no longer needed.
- rotation_x/y/z: compute sin and cos in one `sincos` call and fill the 2×2
  matrix entries directly instead of assembling `cos·I - i·sin·P`; the two-site
  rotations likewise share a single `sincos`.
- phase_shift: build `diag(1, cis(θ))` directly.
- hadamard: build from `RationalRoots.signedroot(1//2)` and promote to eltype.
- Docstrings now use unicode (|0⟩, ⟨0|, ⊗, θ, †, ↦, ↔) where it renders cleanly,
  matching the house style; each parametric gate documents both `θ` and `theta`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Export and document `basis_transform`, matching the other macro-based
  operator modules (index.md advertises it as each module's public entry
  point). Collapse its three per-symmetry methods into the single
  `symmetry::Type{<:Sector}` form used by FermionOperators/BosonOperators;
  the invalid-symmetry error now comes from `qubit_space` for free.
- Factor the repeated `θ`/`theta` angle-resolution idiom (duplicated across
  all nine parametric gates) into a single `_gate_angle` helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants