Block-encoding primitives and semantic execution for classical simulation.
Project description
QuBlock
QuBlock is a lightweight Python library for block-encoding primitives, semantic
simulation on statevectors, and OpenQASM export. The pip package name is
qublock, and the import namespace is blockflow.
Highlights
- Explicit block-encoding invariants with normalization, error tolerance, and success tracking.
- Fast semantic execution for algorithm validation without circuits.
- Recipe-based circuits with declared wire requirements.
- OpenQASM 2 or 3 export with a minimal gate set.
- Small dependency footprint (NumPy only at runtime).
Requirements
- Python 3.9+
- NumPy >= 1.22
Installation
python -m pip install -e .
Dev tools:
python -m pip install -e .[dev]
Quickstart
Semantic execution
import numpy as np
from blockflow import (
ApplyBlockEncodingStep,
BlockEncoding,
NumpyMatrixOperator,
Program,
ResourceEstimate,
SemanticExecutor,
StateVector,
)
mat = np.array([[0, 1], [1, 0]], dtype=complex)
op = NumpyMatrixOperator(mat)
be = BlockEncoding(op=op, alpha=1.0, resources=ResourceEstimate())
program = Program([ApplyBlockEncodingStep(be)])
state = StateVector(np.array([1.0, 0.0], dtype=complex))
final_state, report = SemanticExecutor().run(program, state, renormalize_each_step=True)
Attach a circuit recipe and export QASM
from blockflow import (
Capabilities,
Circuit,
StaticCircuitRecipe,
WireSpec,
)
circ = Circuit(num_qubits=1)
circ.add("h", [0])
recipe = StaticCircuitRecipe(WireSpec(system_qubits=1), circ)
be_with_recipe = BlockEncoding(
op=op,
alpha=1.0,
resources=ResourceEstimate(),
recipe=recipe,
capabilities=Capabilities(supports_circuit_recipe=True),
)
qasm = be_with_recipe.export_openqasm(flavor="qasm3")
Core concepts
- Linear operators: implement the
LinearOperatorprotocol (shape,apply,apply_adjoint). - Block encodings:
BlockEncodingwraps an operator withalpha,epsilon, resources, and recipes. - Vector encodings:
VectorEncodingrepresents normalized state-prep with optional recipes. - Capabilities and success:
CapabilitiesandSuccessModelcapture supported operations and postselection success rates. - Resources:
ResourceEstimatetracks ancillas, depth, and gate counts. - Recipes and circuits:
CircuitRecipeproduces backend-agnosticCircuitobjects with aWireSpec. - QASM export:
to_openqasmemits a minimal gate set to QASM2/QASM3.
Semantic execution
SemanticExecutor applies each program step directly to the system statevector.
This is useful for validating algorithmic structure before committing to a
specific circuit implementation.
program = Program([ApplyBlockEncodingStep(be)])
state = StateVector(np.array([1.0, 0.0], dtype=complex))
final_state, report = SemanticExecutor().run(program, state)
The RunReport accumulates uses, success probabilities, and ancilla peaks.
Circuits, recipes, and OpenQASM
Recipes declare required wires and return a backend-agnostic circuit. The block encoding verifies that the recipe matches its resource claims.
from blockflow import Capabilities, Circuit, StaticCircuitRecipe, WireSpec
circ = Circuit(num_qubits=2)
circ.add("h", [0])
circ.add("cx", [0, 1])
recipe = StaticCircuitRecipe(WireSpec(system_qubits=2), circ)
be = BlockEncoding(
op=op,
alpha=1.0,
resources=ResourceEstimate(),
recipe=recipe,
capabilities=Capabilities(supports_circuit_recipe=True),
)
qasm3 = be.export_openqasm(flavor="qasm3")
qasm2 = be.export_openqasm(flavor="qasm2", optimize=False)
Export currently supports a minimal gate set (h, x, y, z, s, t,
cx, cz, swap, rx, ry, rz, and measure) plus controlled variants in QASM3.
Matrix to block-encoding synthesis (n-qubit)
If you have a 2^n x 2^n matrix, QuBlock can synthesize a block-encoding circuit
without attaching a recipe. There are two paths:
- If
A / alphais unitary (2x2 only), it synthesizes a 1-qubit circuit that implements it. - Otherwise, it builds an LCU block encoding using the Pauli expansion of
A(requiresalpha == sum(|Pauli coeffs|)).
For LCU synthesis you can choose a strategy:
prep_selectusesceil(log2(m))ancillas (plus one phase ancilla if needed) and multi-controlled select gates, wheremis the number of nonzero Pauli terms.sparseuses one ancilla per term and single-controlled select gates.
Both LCU strategies export only to QASM3 because they use controlled rotations.
mat = np.array([[0, 1], [1, 0]], dtype=complex)
be = BlockEncoding(
op=NumpyMatrixOperator(mat),
alpha=1.0,
resources=ResourceEstimate(),
capabilities=Capabilities(supports_circuit_recipe=True),
synthesis_strategy="prep_select", # or "sparse"
)
qasm = be.export_openqasm()
Optimization
Use optimize_circuit to apply simple peephole optimizations, or disable it
when you need a 1:1 recipe export.
optimized = be.build_circuit(optimize=True)
raw = be.build_circuit(optimize=False)
Examples and notebooks
notebooks/lcu_demo.ipynbwalks through LCU synthesis and QASM export.
Development
Tests run with coverage enforcement:
pytest
Ruff is configured for linting and import sorting:
ruff check .
Optional Qiskit cross-check
tests/test_qiskit_integration.py compares QASM output against Qiskit statevector
simulation. Install Qiskit to enable those tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qublock-0.1.0.tar.gz.
File metadata
- Download URL: qublock-0.1.0.tar.gz
- Upload date:
- Size: 27.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82170c2d1666af1c441d46451fcf7c23292e66911eb85d8146fff168102e4b03
|
|
| MD5 |
4a42daa0e34e0b1cbc5fea8d23217883
|
|
| BLAKE2b-256 |
22cbaf2768f7006812dd9c2d5804ca3393f437b9654ddca0a0e0727f9bfe8f7b
|
Provenance
The following attestation bundles were made for qublock-0.1.0.tar.gz:
Publisher:
python-publish.yml on EthanFeld/QuBlock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
qublock-0.1.0.tar.gz -
Subject digest:
82170c2d1666af1c441d46451fcf7c23292e66911eb85d8146fff168102e4b03 - Sigstore transparency entry: 782111773
- Sigstore integration time:
-
Permalink:
EthanFeld/QuBlock@327a41bea4785011944699fb9ca159542ce4f79d -
Branch / Tag:
refs/tags/PyPiProblems - Owner: https://github.com/EthanFeld
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@327a41bea4785011944699fb9ca159542ce4f79d -
Trigger Event:
release
-
Statement type:
File details
Details for the file qublock-0.1.0-py3-none-any.whl.
File metadata
- Download URL: qublock-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf0501e0edbad4fb0198b439d30d74a72ffffa8fc642bd22421651f1d0399763
|
|
| MD5 |
32895ca38bee131ffb25ae40d4a97096
|
|
| BLAKE2b-256 |
29f585598d41af3ef4651f8c6d785e474e2b7b7343485cf4d4337845df76f657
|
Provenance
The following attestation bundles were made for qublock-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on EthanFeld/QuBlock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
qublock-0.1.0-py3-none-any.whl -
Subject digest:
cf0501e0edbad4fb0198b439d30d74a72ffffa8fc642bd22421651f1d0399763 - Sigstore transparency entry: 782111778
- Sigstore integration time:
-
Permalink:
EthanFeld/QuBlock@327a41bea4785011944699fb9ca159542ce4f79d -
Branch / Tag:
refs/tags/PyPiProblems - Owner: https://github.com/EthanFeld
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@327a41bea4785011944699fb9ca159542ce4f79d -
Trigger Event:
release
-
Statement type: