operators-and-transforms
Operate on OpenFermion's symbolic and tensor operator representations, normalize and validate terms, and choose or apply fermion-to-qubit mappings.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- operators-and-transforms
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Operate on OpenFermion's symbolic and tensor operator representations, normalize and validate terms, and choose or apply fermion-to-qubit mappings.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md10.5 KB
- 参考文档references/transforms.md8.6 KB
- 参考文档references/troubleshooting.md8.6 KB
- 脚本scripts/smoke_transform.py2.9 KB
03 SKILL.md 正文
Operators and transforms
Use this sub-skill when a task needs second-quantized operator algebra, Pauli strings, Majorana or bosonic ladder operators, tensor-backed interaction representations, normal ordering, Hermitian checks, commutators/BCH terms, or a fermion-to-qubit mapping. It covers the public OpenFermion APIs and small, inspectable operators; route model construction and chemistry file formats to hamiltonians-and-chemistry, circuit synthesis/simulation to circuits-and-simulation, and large eigensolvers or RDM workflows to analysis-and-measurements.
Fast route
- Identify the representation. Use
FermionOperatorfor arbitrary fermionic ladder-polynomials,QubitOperatorfor sums of Pauli strings,BosonOperatorfor bosonic ladders, andMajoranaOperatorfor products of Majorana generators. UseInteractionOperatororPolynomialTensoronly when fixed tensor structure and array operations are useful. - Construct with explicit conventions. Fermion/boson strings use
p^for creation andpfor annihilation. Qubit strings useX0,Y1, orZ2. The empty string/tuple is identity; a no-argument constructor is zero. Keep the factor order of fermionic terms untilnormal_orderedis called. - Validate before mapping. Check
count_qubits(op),is_hermitian(op), and the relevant number-preservation predicate. For malformed input, inspect index/action types first; see troubleshooting. - Choose a map deliberately. Use
jordan_wignerfor the direct canonical mapping and broad compatibility;bravyi_kitaevfor a Fenwick-tree mapping with an optional explicit qubit count;binary_code_transformfor a suppliedBinaryCode(including parity, checksum, or qubit-saving codes). Usereverse_jordan_wigneronly for the JW inverse direction. - Compare representations, not term dictionaries. JW and BK generally produce different Pauli strings but represent equivalent operators. Compare spectra or sparse matrices only after choosing the same Hilbert-space size.
- Cross the sparse boundary intentionally. Use
get_sparse_operatoronly after mapping/normalization and passn_qubitsfor padded fermion/qubit spaces. For bosons, pass a finitetrunc; this is a Fock-space cutoff, not a qubit count.
Core recipes
from openfermion import (
FermionOperator, QubitOperator, bravyi_kitaev, count_qubits,
hermitian_conjugated, jordan_wigner, normal_ordered,
)
hop = FermionOperator("0^ 1", 0.5)
hop += hermitian_conjugated(hop)
hop = normal_ordered(hop)
assert hop.is_normal_ordered()
print(count_qubits(hop)) # 2
jw = jordan_wigner(hop) # 0.25 [X0 X1] + 0.25 [Y0 Y1]
bk = bravyi_kitaev(hop, n_qubits=2) # equivalent, different strings
Use op.terms for programmatic inspection. Its keys are tuples of
(index, action) pairs: (index, 1)/(index, 0) for creation/annihilation and
(index, "X"|"Y"|"Z") for Pauli factors. +=, -=, *=, and /= mutate;
ordinary arithmetic returns a copy. Same-type products are algebraically
simplified, including Pauli phases. compress() removes near-zero terms;
isclose(other, rtol=..., atol=...) is preferable to comparing printed text.
For a quick CPU-only smoke check from any working directory, run the bundled smoke_transform.py helper:
python scripts/smoke_transform.py --help
python scripts/smoke_transform.py --mapping jw --sparse
The helper uses only public imports, a two-mode hopping term, and a tiny sparse matrix; it does not read source files or use the network.
Decision points and boundaries
normal_orderedis a representation rewrite using canonical anti-commutation/commutation relations; it can increase term count and is exponential in the locality of a ladder term. For chemistry-style two-body operators,chemist_orderedrequires a two-body, number-conservingFermionOperator.InteractionOperatorassumes a number-conserving, Hermitian interaction Hamiltonian structure. It is not a general replacement for an arbitraryFermionOperator; use the latter for non-Hermitian or non-number-conserving expressions.jordan_wignermapsFermionOperator,MajoranaOperator,InteractionOperator, and diagonal Coulomb representations, but has non_qubitsargument.bravyi_kitaevacceptsFermionOperator,MajoranaOperator, andInteractionOperator; itsn_qubitsmay pad the first two kinds. Never pass a value belowcount_qubits.- Custom binary codes are constrained by the code's
n_modesandn_qubits; they are not a general way to pad an operator. Nonlinear or qubit-saving codes can expand a term into many Pauli strings. reverse_jordan_wigneraccepts only aQubitOperatorand returns aFermionOperator; it is not a general inverse for BK or arbitrary binary codes. Round-trip aftercompress()is the useful correctness check.- Sparse conversion is a representation boundary, not a solver. Keep large eigenspectrum, ground-state, RDM, measurement, and model-builder work in the routed sub-skills.
For signatures, term schemas, tensor indexing, and concrete expected outputs, read api-reference.md. For mapping formulas, variants, qubit-count rules, and binary-code choices, read transforms.md. For failure diagnosis, read troubleshooting.md.
Recovery guidance
When recovering an operator transformation from partial notes, first reconstruct
the exact term tuple and coefficient, then normal-order it and record
count_qubits. Build the smallest Hermitian hopping or number-operator fixture,
map it with JW and the selected alternative, and compare canonical algebra
(commutator/anticommutator), term counts, and—when feasible—small sparse
matrices. Do not infer a BK or binary-code qubit count from a printed term:
carry the explicit n_qubits/code dimensions through the experiment. Preserve
whether a mismatch is a convention issue (mode order, creation order, or
Majorana indexing), an invalid input, or a true transform limitation.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).