Quantum Phase Estimation implementation.
Example:
@qmc.qkernel
def p_gate(q: qmc.Qubit, theta: float) -> qmc.Qubit:
return qmc.p(q, theta)
@qmc.qkernel
def circuit(theta: float) -> qmc.Float:
counting = qmc.qubit_array(3, name="counting")
target = qmc.qubit(name="target")
target = qmc.x(target)
phase = qmc.qpe(target, counting, p_gate, theta=theta)
return qmc.measure(phase)Overview¶
| Function | Description |
|---|---|
for_loop | Create a traced for loop in the Qamomile frontend. |
qpe | Quantum Phase Estimation. |
| Class | Description |
|---|---|
QKernelLike | Describe the frontend surface required by compiler entrypoints. |
Functions¶
for_loop [source]¶
def for_loop(
start,
stop,
step = 1,
var_name: str = '_loop_idx',
*,
captures: tuple[tuple[str, Any], ...] = (),
) -> Generator[UInt, None, None]Create a traced for loop in the Qamomile frontend.
Parameters:
| Name | Type | Description |
|---|---|---|
start | typing.Any | Inclusive loop start as an integer or UInt. |
stop | typing.Any | Exclusive loop stop as an integer or UInt. |
step | typing.Any | Nonzero loop step as an integer or UInt. Defaults to 1. |
var_name | str | Display name of the loop variable. Defaults to "_loop_idx". |
captures | tuple[tuple[str, typing.Any], ...] | Statically analyzed read-only body inputs. Defaults to an empty tuple. |
Yields:
UInt — The loop iteration variable (can be used as array index)
Raises:
TypeError— If a bound cannot be represented as a scalar IR value.ValueError— If the constructed loop has inconsistent region-result metadata.
Example:
@QKernel
def my_kernel(qubits: Array[Qubit, Literal[3]]) -> Array[Qubit, Literal[3]]:
for i in qm.range(3):
qubits[i] = h(qubits[i])
return qubits
@QKernel
def my_kernel2(qubits: Array[Qubit, Literal[5]]) -> Array[Qubit, Literal[5]]:
for i in qm.range(1, 4): # i = 1, 2, 3
qubits[i] = h(qubits[i])
return qubitsClassical scalar updates (total = total + i) become explicit
RegionArg records on the ForOperation: the loop enters with
the initializer, each iteration reads the previous iteration’s
value, and post-loop code reads the loop result.
qpe [source]¶
def qpe(
target: Qubit,
counting: Vector[Qubit],
unitary: QKernelLike,
**params: Any = {},
) -> QFixedQuantum Phase Estimation.
Estimates the phase φ where U|ψ> = e^{2πiφ}|ψ>.
Parameters:
| Name | Type | Description |
|---|---|---|
target | Qubit | Eigenstate |psi> of the unitary. |
counting | Vector[Qubit] | Register that stores the phase estimate. |
unitary | QKernelLike | Unitary qkernel to control. |
**params | Any | Classical parameters forwarded to the unitary. |
Returns:
QFixed — Phase register as quantum fixed-point number
Classes¶
QKernelLike [source]¶
class QKernelLike(Protocol)Describe the frontend surface required by compiler entrypoints.
This protocol is intentionally structural. It lets decorator-created
composites reuse the qkernel inspection and build interface without making
them inherit from QKernel or exposing the compiler-facing callable
descriptor model as a frontend concept.
Attributes¶
block: Block Return the cached hierarchical body block.effects: KernelEffect Return cached semantic effects of the qkernel body.input_types: dict[str, Any] Return frontend input annotations by parameter name.name: str Return the user-facing callable name.output_types: list[Any] Return frontend output annotations.signature: inspect.Signature Return the Python call signature.
Methods¶
build¶
def build(self, parameters: list[str] | None = None, **kwargs: Any = {}) -> BlockBuild a traced body block.
Parameters:
| Name | Type | Description |
|---|---|---|
parameters | list[str] | None | Runtime parameter names to preserve. Defaults to None. |
**kwargs | Any | Compile-time bindings for non-parameter arguments. |
Returns:
Block — Traced hierarchical body block.