ket.quantumstate¶
Quantum state snapshot.
This module provides the base class for a snapshot of a quantum state obtained using a quantum simulator.
Classes ket.quantumstate
¶
Snapshot of a quantum state. |
- class QuantumState(qubits: Quant)¶
Snapshot of a quantum state.
This class holds a reference for a snapshot of a quantum state obtained using a quantum simulator. The result may not be available right after the measurement call, especially in batch execution.
You can instantiate this class by calling the
dump
function.- Example:
from ket import * p = Process() a, b = p.alloc(2) with around(cat(kron(H, I), CNOT), a, b): Y(a) inside = dump(a+b) outside = dump(a+b) print(inside.show()) # |01⟩ (50.00%) # -0.707107i ≅ -i/√2 # |10⟩ (50.00%) # 0.707107i ≅ i/√2 print(outside.show()) # |11⟩ (100.00%) # -1.000000i ≅ -i/√1
- Parameters:
qubits – Qubits from which to capture a quantum state snapshot.
- property states: dict[int, complex] | None¶
Get the quantum state.
Returns a dictionary mapping base states to their corresponding probability amplitudes.
- Returns:
The quantum state, or None if the quantum state information is not available.
- get() dict[int, complex] ¶
Get the quantum state.
If the quantum state is not available, the quantum process will execute to get the result.
- property probabilities: dict[int, float] | None¶
Get the measurement probabilities.
Returns a dictionary mapping base states to their corresponding measurement probabilities.
- Returns:
The measurement probabilities, or None if the quantum state information is not available.
- sample(shots=4096, seed=None) dict[int, int] | None ¶
Get the quantum execution shots.
The parameters
shots
andseed
are used to generate the sample from the quantum state snapshot.- Parameters:
shots – Number of shots. Defaults to 4096.
seed – Seed for the RNG.
- Returns:
A dictionary mapping measurement outcomes to their frequencies in the generated sample, or None if the quantum state information is not available.
- sphere() Figure ¶
Generate a Bloch sphere plot representing the quantum state.
This method creates a Bloch sphere plot visualizing of one qubit quantum state.
Note
This method requires additional dependencies from
ket-lang[visualization]
.Install with:
pip install ket-lang[visualization]
.- Returns:
A Bloch sphere plot illustrating the quantum state.
- show(format_str: str | None = None, mode: Literal['latex', 'str'] | None = None) str | Math ¶
Return the quantum state as a string.
Use the format string to change the print format of the basis states:
i
: print the state in the decimal baseb
: print the state in the binary base (default)i|b<n>
: separate then
first qubits; the remaining print in the binary basei|b<n1>:i|b<n2>[:i|b<n3>...]
: separate then1, n2, n3, ...
first qubits
- Example:
from ket import * p = Process() q = p.alloc(19) X(ctrl(H(q[0]), X, q[1:])[1::2]) d = dump(q) print(d.show('i')) # |87381⟩ (50.00%) # 0.707107 ≅ 1/√2 # |436906⟩ (50.00%) # 0.707107 ≅ 1/√2 print(d.show('b')) # |0010101010101010101⟩ (50.00%) # 0.707107 ≅ 1/√2 # |1101010101010101010⟩ (50.00%) # 0.707107 ≅ 1/√2 print(d.show('i4')) # |2⟩|101010101010101⟩ (50.00%) # 0.707107 ≅ 1/√2 # |13⟩|010101010101010⟩ (50.00%) # 0.707107 ≅ 1/√2 print(d.show('b5:i4')) # |00101⟩|5⟩|0101010101⟩ (50.00%) # 0.707107 ≅ 1/√2 # |11010⟩|10⟩|1010101010⟩ (50.00%) # 0.707107 ≅ 1/√2
- Parameters:
format – Format string that matches
(i|b)\d*(:(i|b)\d+)*
.mode – If
"str"
, return a string representation of the quantum state. If"latex"
, return a LaTeX Math representation of the quantum state. If in Jupyter Notebook, defaults to"latex"
, otherwise defaults to"str"
.
- Returns:
The formatted quantum state as a string, or Latex Math.
- histogram(**kwargs) Figure ¶
Generate a histogram representing the quantum state.
This method creates a histogram visualizing the probability distribution of the quantum state.
Note
This method requires additional dependencies from
ket-lang[visualization]
.Install with:
pip install ket-lang[visualization]
.- Returns:
Histogram of the quantum state.