Circuit machinery

Circuit machinery#

Turns a Yao-style gate list into a JAX einsum, and caches the contraction path and the JIT-compiled closure. Every basis uses it.

Circuit machinery: einsum builder + JIT closure cache.

Shared by every basis. The builder converts a Yao-style gate list into a JAX einsum (Hadamard-first sort + Yao little-endian ordering preserved). The cache memoizes jnp.einsum_path results and the JIT’d closures.

Contents

build_circuit_einsum

Convert a gate sequence to (subscripts, tensors, shapes).

compile_circuit

Build a JIT-compiled stepped circuit closure.

apply_circuit

Contract pic through the circuit and reshape back to (2^m, 2^n).

optimize_code_cached

Return a jit-compiled einsum closure bound to fixed subscripts and shapes.

sorted_gate_program

Return (kind, qubits) per tensor in sorted operand order.

Gate

One gate of a circuit program.

HADAMARD

controlled_phase_diag

2x2 compact representation of a 4x4 controlled-phase gate.

u4_from_phase

Return the 2-qubit (2, 2, 2, 2)-shaped tensor matching the 4×4 controlled- phase gate diag(1, 1, 1, exp(iφ)).

extract_phase_from_cp

Extract φ from a compact 2x2 CP tensor [[1, 1], [1, e^iφ]].

is_compact_cp

True if tensor looks like a compact 2x2 CP gate [[1, 1], [1, e^iφ]].

select_last_n_cp_indices

Return indices of the LAST n_gates compact-CP tensors in tensors.