Make Quantum an executable handbook with verified experiments

This commit is contained in:
cflmy committed 2026-11-08 21:50:01 +08:00
1 parent bb06dad134
commit cc5277d2b5
23 files changed
+2990 -696

No files matched your search

+124 -33
View File
@@ -7,32 +7,53 @@ import:
cap: ext/quantum/_capability/index.mq.md
---
Density
Density operators — inspect a subsystem or a mixed-state matrix.
A density operator describes pure and mixed states together. Pure states have
purity one; mixing lowers purity. Build rho from a circuit or state, or from an
explicit Hermitian matrix. Partial trace discards subsystems. Expectation values
take Pauli strings or matrices.
Build rho from an ideal circuit/state with from_state, or from a square
Hermitian 2^n matrix with from_matrix. Density operations and Schmidt support
at most six qubits. The matrix constructor checks shape and Hermiticity; it
does not certify positive semidefiniteness or trace one. For physical inputs,
check the trace and eig eigenvalues yourself before interpreting purity.
## For AI agents
If you were given only this file, build or inspect density operators with marqdo
call and --effects plugin.
An independent mixed-state check
Call example (plain text):
marqdo call ext/quantum/density/index.mq.md from_state state=$qc --effects plugin --toget rho --json
marqdo call ext/quantum/density/index.mq.md purity density=$rho --effects plugin --json
This one-qubit maximally mixed matrix has trace one, purity 0.5 and Z
expectation zero. The complete example needs no prior circuit or saved handle.
For entanglement, start from a Bell circuit, call from_state, then partial_trace
with keep=[0]; purity of that reduced density is also 0.5.
Author programs may use the density object on this file.
```sh
marqdo call ext/quantum/density/index.mq.md from_matrix 'matrix=[[0.5,0],[0,0.5]]' --effects plugin --session handbook --toget rho --json
marqdo call ext/quantum/density/index.mq.md purity 'density=$rho' --effects plugin --session handbook --json
marqdo call ext/quantum/density/index.mq.md expect 'density=$rho' 'obs="Z"' --effects plugin --session handbook --json
marqdo call ext/quantum/density/index.mq.md eig 'density=$rho' --effects plugin --session handbook --json
```
partial_trace keeps the listed zero-based wires and discards the others.
Pauli strings put the highest bit on the left; ZZ measures correlation on two
wires. matrix exports nested complex entries, and eig returns eigenvalues and
eigenvectors. Use [viz.draw_density](../viz/index.mq.md) for hinton, city,
density or paulivec plots. from_state ignores attached teaching noise, just
like the ideal simulator; it is not a noisy density evolution engine.
Development route
Add mixed-state mathematics to plugins/quantum/src/linalg.rs, export it through
src/lib.rs and update this file plus its Chinese mirror. Keep physical input
validation claims aligned with the constructor. Validate pure-state purity
one, Bell reduced purity one-half and observable expectations against
independent matrices in tests/ext/quantum-linalg-smoke.mq.md.
## from_state
Form rho as the outer product of a pure `state` (or the ideal state of a
circuit). Use this when you trust the pure-state picture and want mixed-state
tools without writing a matrix by hand.
| 参数 | 类型 | 说明 |
|------|------|------|
| state | any | pure state or circuit |
Form rho as the outer product of a pure `state` (or the ideal state of a
circuit). Use this when you trust the pure-state picture and want mixed-state
tools without writing a matrix by hand.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | density handle |
@@ -41,14 +62,15 @@ tools without writing a matrix by hand.
*> quantum_density_from_state state=`state`*
## from_matrix
Build rho from an explicit Hermitian `matrix`. Pair with formula blocks in the
lecture so the matrix on the page is the matrix in the simulator. Size is
limited by the plugin.
| 参数 | 类型 | 说明 |
|------|------|------|
| matrix | any | Hermitian matrix |
Build rho from an explicit Hermitian `matrix`. Pair with formula blocks in the
lecture so the matrix on the page is the matrix in the simulator. Size is
limited by the plugin.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | density handle |
@@ -57,13 +79,14 @@ limited by the plugin.
*> quantum_density_from_matrix matrix=`matrix`*
## purity
Compute the trace of rho squared for `density`. Values near one mean nearly
pure; lower values mean classical mixing or traced-out entanglement.
| 参数 | 类型 | 说明 |
|------|------|------|
| density | any | density operator |
Compute the trace of rho squared for `density`. Values near one mean nearly
pure; lower values mean classical mixing or traced-out entanglement.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | number | purity |
@@ -72,15 +95,16 @@ pure; lower values mean classical mixing or traced-out entanglement.
*> quantum_density_purity density=`density`*
## partial_trace
Keep listed qubits in `keep` and trace out the rest of `density`. Indices use
LSB equals zero. The result is generally mixed even when the joint state was
pure.
| 参数 | 类型 | 说明 |
|------|------|------|
| density | any | joint density |
| keep | any | qubit index list to keep |
Keep listed qubits in `keep` and trace out the rest of `density`. Indices use
LSB equals zero. The result is generally mixed even when the joint state was
pure.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | reduced density |
@@ -89,14 +113,15 @@ pure.
*> quantum_density_partial_trace density=`density` keep=`keep`*
## expect
Expectation of observable `obs` on `density`. Pauli strings use left equals
high bit. Pass a matrix when the observable is not a Pauli product.
| 参数 | 类型 | 说明 |
|------|------|------|
| density | any | density operator |
| obs | any | Pauli string or matrix |
Expectation of observable `obs` on `density`. Pauli strings use left equals
high bit. Pass a matrix when the observable is not a Pauli product.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | number | expectation value |
@@ -105,13 +130,14 @@ high bit. Pass a matrix when the observable is not a Pauli product.
*> quantum_density_expect density=`density` obs=`obs`*
## eig
Hermitian spectrum of `density` as eigenvalues and eigenvectors. Useful when a
lecture needs the eigenbasis of rho.
| 参数 | 类型 | 说明 |
|------|------|------|
| density | any | Hermitian density |
Hermitian spectrum of `density` as eigenvalues and eigenvectors. Useful when a
lecture needs the eigenbasis of rho.
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | eigenvalues and eigenvectors |
@@ -119,9 +145,35 @@ lecture needs the eigenbasis of rho.
> cap.load
*> quantum_density_eig density=`density`*
## matrix
| 参数 | 类型 | 说明 |
|------|------|------|
| density | any | Density handle / 密度句柄 |
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | Nested complex entries with re and im |
Export the explicit nested complex matrix of `density`. Reading entries does not
change the density operator. Use the exported entries for an independent trace
check or further linear algebra; the caller remains responsible for physicality.
> cap.load
*> quantum_density_matrix density=`density`*
# density
+ `state`=None
+ `matrix`=None
| 参数 | 类型 | 说明 |
|------|------|------|
| state | any | Default None |
| matrix | any | Default None |
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | Returned value |
Inputs: `state`=None, `matrix`=None (input).
Construct a density operator from a pure state or circuit (state=) or from an explicit Hermitian matrix=. Density operations support at most six qubits. Pure and mixed states share this object surface for purity, partial trace, and expectation.
@@ -133,18 +185,35 @@ Construct a density operator from a pure state or circuit (state=) or from an ex
## matrix
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | Nested complex entries with re and im |
Return the explicit matrix of this density operator so authors can inspect entries or hand the array to further linear-algebra steps outside the quantum package.
*> quantum_density_matrix density=`self`*
## purity
| 返回 | 类型 | 说明 |
|------|------|------|
| result | number | Scalar observation |
Compute Tr(ρ²). Values near one mean a pure state; mixed states are strictly less than one. Use this as a quick entanglement or decoherence check after a partial trace.
*> quantum_density_purity density=`self`*
## partial_trace
+ `keep`
| 参数 | 类型 | 说明 |
|------|------|------|
| keep | any | Required |
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | Density operator handle |
Inputs: `keep` (input).
Trace out every qubit except those listed in keep (LSB index 0). Returns the reduced density on the kept subsystem for entanglement lectures and purity checks.
@@ -152,20 +221,42 @@ Trace out every qubit except those listed in keep (LSB index 0). Returns the red
## eig
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | eigenvalues and eigenvectors |
Compute the Hermitian spectrum of this density operator as eigenvalues and eigenvectors. Handy for lecture plots and for verifying that a constructed rho is physical.
*> quantum_density_eig density=`self`*
## expect
+ `obs`
| 参数 | 类型 | 说明 |
|------|------|------|
| obs | any | Required |
| 返回 | 类型 | 说明 |
|------|------|------|
| result | number | Scalar observation |
Inputs: `obs` (input).
Expectation value of observable obs on this density. obs may be a Pauli string with the left end as the high bit, or an explicit matrix matching the density dimension.
*> quantum_density_expect density=`self` obs=`obs`*
## draw
+ `path`=None
+ `kind`=hinton
| 参数 | 类型 | 说明 |
|------|------|------|
| path | any | Default None |
| kind | any | Default hinton |
| 返回 | 类型 | 说明 |
|------|------|------|
| result | any | quantum_svg with kind and svg |
Inputs: `path`=None, `kind`="hinton" (input).
Draw a Hinton diagram, city plot, density grid, or Pauli vector for this density. Optional path writes SVG; kind selects which visual metaphor the lecture uses.