Make Quantum an executable handbook with verified experiments
This commit is contained in:
1 parent
bb06dad134
commit
cc5277d2b5
23 files changed
+2990
-696
No files matched your search
+116
-26
@@ -7,25 +7,44 @@ import:
|
||||
cap: ext/quantum/_capability/index.mq.md
|
||||
---
|
||||
|
||||
Gate
|
||||
Gates — define the operator before choosing the wires it acts on.
|
||||
|
||||
A gate is a unitary on one or more qubits. This skill writes the matrix in
|
||||
lecture form, then builds a gate handle. Circuits apply gates; this page defines
|
||||
what they are. Custom unitaries use the same path with a matrix argument from a
|
||||
formula block or nested list.
|
||||
Use named for I, X, Y, Z, H, S, T, Rx, Ry, Rz, CX, CZ or SWAP. Rotations
|
||||
require theta in radians. A custom gate is a square 2^k matrix satisfying
|
||||
U†U = I (construction tolerance 1e-6). Cells may be real numbers or maps with
|
||||
re and im fields. JSON and a bound Markdown formula reach the same constructor.
|
||||
|
||||
## For AI agents
|
||||
If you were given only this file, run tools with marqdo call and --effects plugin.
|
||||
FUNCTION is the ## name. Example Hadamard gate handle:
|
||||
A complete custom-gate experiment
|
||||
|
||||
Call example (plain text):
|
||||
marqdo call ext/quantum/gate/index.mq.md hadamard --effects plugin --json
|
||||
The matrix below swaps zero and one. matches must return true, and the final
|
||||
probabilities must put all weight on basis one. This uses the
|
||||
[circuit module](../circuit/index.mq.md) to apply the operator; constructing a
|
||||
gate alone never changes a circuit. All references belong to the same session.
|
||||
|
||||
Custom unitary: pass matrix= as JSON or a prior toget. matches checks a gate
|
||||
against a matrix. Author programs may also use the gate object constructor on
|
||||
this file.
|
||||
```sh
|
||||
marqdo call ext/quantum/gate/index.mq.md custom 'matrix=[[0,1],[1,0]]' 'name="X-demo"' --effects plugin --session handbook --toget u --json
|
||||
marqdo call ext/quantum/gate/index.mq.md matches 'gate=$u' 'matrix=[[0,1],[1,0]]' --effects plugin --session handbook --json
|
||||
marqdo call ext/quantum/circuit/index.mq.md new qubits=1 --effects plugin --session handbook --toget qc --json
|
||||
marqdo call ext/quantum/circuit/index.mq.md apply 'circuit=$qc' 'gate=$u' qubits=0 --effects plugin --session handbook --toget qc --json
|
||||
marqdo call ext/quantum/simulate/index.mq.md probabilities 'circuit=$qc' --effects plugin --session handbook --json
|
||||
```
|
||||
|
||||
Choose hadamard, pauli_x, pauli_z or cnot for a familiar constant; named covers
|
||||
the other built-ins, custom accepts a formula matrix, matrix exports entries,
|
||||
and matches checks that the explanation agrees with the implemented operator.
|
||||
Use [viz.draw_gate](../viz/index.mq.md) for a glyph or matrix heatmap. Prefer
|
||||
circuit.cnot with explicit control and target for an entangling operation.
|
||||
|
||||
Development route
|
||||
|
||||
For a new user-defined unitary, use custom and an independent expected matrix;
|
||||
no Rust or ABI change is necessary. For a new built-in, update
|
||||
plugins/quantum/src/sim.rs::named_gate_matrix and its numerical tests before
|
||||
exposing it here and in the Chinese mirror. Preserve tests/ext/quantum-custom-gate-smoke.mq.md,
|
||||
which binds the Hadamard formula directly into the author gate constructor.
|
||||
|
||||
## hadamard
|
||||
|
||||
The Hadamard gate creates equal superposition from computational basis states.
|
||||
Its matrix in the computational basis is one over square-root two times the
|
||||
matrix with ones on the first row and one then minus one on the second row.
|
||||
@@ -42,6 +61,7 @@ This tool takes no parameters.
|
||||
*> quantum_gate_new name="H" theta=None matrix=None*
|
||||
|
||||
## pauli_x
|
||||
|
||||
Pauli X is the bit-flip gate: a two-by-two matrix that swaps ket-zero and
|
||||
ket-one. Use it when a lecture needs an explicit NOT on the computational basis.
|
||||
|
||||
@@ -55,6 +75,7 @@ This tool takes no parameters.
|
||||
*> quantum_gate_new name="X" theta=None matrix=None*
|
||||
|
||||
## pauli_z
|
||||
|
||||
Pauli Z applies a phase of minus one to ket-one while leaving ket-zero alone.
|
||||
Its diagonal matrix is the standard computational-basis Z.
|
||||
|
||||
@@ -68,6 +89,7 @@ This tool takes no parameters.
|
||||
*> quantum_gate_new name="Z" theta=None matrix=None*
|
||||
|
||||
## cnot
|
||||
|
||||
The controlled-NOT on two qubits flips the target when the control is ket-one.
|
||||
Wire order and bit-endianness follow the package convention where qubit 0 is the
|
||||
rightmost label bit. Prefer applying via circuit tools with explicit control and
|
||||
@@ -83,15 +105,16 @@ This tool takes no parameters.
|
||||
*> quantum_gate_new name="CX" theta=None matrix=None*
|
||||
|
||||
## custom
|
||||
Build a gate from an explicit unitary given as `matrix`. Authors usually bind a
|
||||
formula matrix first, then pass that binding. Optional label `name`="U" marks
|
||||
drawings when the caller omits a name.
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| matrix | any | unitary matrix formula or list |
|
||||
| name | text | optional label defaulting to U |
|
||||
|
||||
Build a gate from an explicit unitary given as `matrix`. Authors usually bind a
|
||||
formula matrix first, then pass that binding. Optional label `name`="U" marks
|
||||
drawings when the caller omits a name.
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | custom gate handle |
|
||||
@@ -100,9 +123,6 @@ drawings when the caller omits a name.
|
||||
*> quantum_gate_new name=`name` theta=None matrix=`matrix`*
|
||||
|
||||
## matches
|
||||
Teaching check: does `gate` match expected `matrix` within absolute tolerance
|
||||
`tol`=0.000000001? Use after defining a formula so the lecture and the simulator
|
||||
cannot drift apart.
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
@@ -110,6 +130,10 @@ cannot drift apart.
|
||||
| matrix | any | expected unitary |
|
||||
| tol | number | absolute tolerance |
|
||||
|
||||
Teaching check: does `gate` match expected `matrix` within absolute tolerance
|
||||
`tol`=0.000000001 ? Use after defining a formula so the lecture and the simulator
|
||||
cannot drift apart.
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | bool | True when matrices match |
|
||||
@@ -117,10 +141,54 @@ cannot drift apart.
|
||||
> cap.load
|
||||
*> quantum_gate_matches_matrix gate=`gate` matrix=`matrix` tol=`tol`*
|
||||
|
||||
## named
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| name | text | Built-in gate name |
|
||||
| theta | any | Rotation angle or None |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | Gate handle for circuit.apply |
|
||||
|
||||
Construct built-in `name`="H" with optional `theta`=None . Rx, Ry and Rz require
|
||||
a numeric angle in radians. The returned gate can be passed to circuit.apply;
|
||||
creating it never changes a circuit. Supported names are listed above.
|
||||
|
||||
> cap.load
|
||||
*> quantum_gate_new name=`name` theta=`theta` matrix=None*
|
||||
|
||||
## matrix
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| gate | any | Gate handle |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | Nested complex entries with re and im |
|
||||
|
||||
Export the explicit unitary of `gate`. Each complex entry has re and im fields.
|
||||
Use this to compare a formula with independent expected entries. Reading a gate
|
||||
matrix neither modifies its handle nor applies the operator to any circuit.
|
||||
|
||||
> cap.load
|
||||
*> quantum_gate_matrix gate=`gate`*
|
||||
|
||||
# gate
|
||||
+ `name`=None
|
||||
+ `theta`=None
|
||||
+ `matrix`=None
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| name | any | Default None |
|
||||
| theta | number | Default None |
|
||||
| matrix | any | Default None |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | Gate handle for circuit.apply |
|
||||
|
||||
Inputs: `name`=None, `theta`=None, `matrix`=None (input).
|
||||
|
||||
Named built-in gate (name=H) or a custom unitary defined by matrix= (nested lists or a formula matrix fence). Custom gates may set name= (default U). Authors and call-style tools share this object surface.
|
||||
|
||||
@@ -129,21 +197,43 @@ Named built-in gate (name=H) or a custom unitary defined by matrix= (nested list
|
||||
|
||||
## matrix
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | Nested complex entries with re and im |
|
||||
|
||||
Return the explicit unitary matrix of this gate handle. Use it to inspect entries or to feed matches_matrix after constructing a gate from a formula.
|
||||
|
||||
*> quantum_gate_matrix gate=`self`*
|
||||
|
||||
## matches_matrix
|
||||
+ `matrix`
|
||||
+ `tol`=0.000000001
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| matrix | any | Required |
|
||||
| tol | any | Default 0.000000001 |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | bool | Match within absolute tolerance |
|
||||
|
||||
Inputs: `matrix`, `tol`=0.000000001 (input).
|
||||
|
||||
Check whether this gate matches the given matrix within absolute tolerance tol. Typical after building a custom gate from a Markdown formula fence in a lecture notebook.
|
||||
|
||||
*> quantum_gate_matches_matrix gate=`self` matrix=`matrix` tol=`tol`*
|
||||
|
||||
## draw
|
||||
+ `path`=None
|
||||
+ `kind`=gate
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| path | any | Default None |
|
||||
| kind | any | Default gate |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| result | any | quantum_svg with kind and svg |
|
||||
|
||||
Inputs: `path`=None, `kind`="gate" (input).
|
||||
|
||||
Draw a gate symbol or a complex-matrix heatmap (kind=gate or matrix). Optional path writes SVG to disk; plots also enter view/CLI through record_plot for the session.
|
||||
|
||||
|
||||
Reference in new issue
Block a user