ket.operations¶
Functions to manipulate quantum operations.
This module provides essential functions in the Ket library for manipulating quantum operations. It includes functions for controlled and inverse operations, facilitating quantum circuit construction.
Functions ket.operations
¶
|
Create a controlled gate |
|
Return the inverse of a gate. |
|
Applying and then reversing quantum gates. |
|
Concatenate gates. |
|
Controlled scope. |
|
Add control qubits to a gate. |
|
Obtain the quantum state snapshot. |
|
Calculate the expected value for a quantum state. |
|
Inverse scope. |
|
Force to consider as a diagonal gate. |
|
Force to consider as a permutation gate. |
|
Gates tensor product. |
|
Measure the given qubits and return a measurement object. |
|
Get the quantum state measurement samples. |
|
Add axillary qubits to a quantum gate. |
- C(gate: Callable) Callable ¶
Create a controlled gate
- Parameters:
gate – Quantum gate.
- Returns:
A gate with the first parameter as control.
- adj(gate: Callable[[Any], Any]) Callable[[Any], Any] ¶
Return the inverse of a gate.
Create a new callable that applies the inverse of the given
gate
.The resulting callable will iterate over the parameters to identify the
Process
that the inverse operation will be applied. If non of the parameters are aQuant
, the keyword argumentket_process
must be given.- Usage:
from ket import * p = Process() a, b = q.alloc(2) bell = cat(kron(H, I), CNOT) adj(bell)(a, b)
- Parameters:
gate – The gate to apply the inverse.
- Returns:
A new callable that applies the inverse of the given gate.
- around(gate: Callable, *args, ket_process: Process | None = None, **kwargs)¶
Applying and then reversing quantum gates.
Apply the given quantum gate (\(U\)) and then execute a code block (\(V\)). After the code block is executed, the inverse of the gate (\(U^\dagger\)) is applied, resulting in \(UVU^\dagger\).
Example
from ket import * p = Process() a, b = p.alloc(2) bell = cat(kron(H, I), CNOT) with around(bell, a, b): # Apply bell(a, b) X(q) # Execute the code block # Apply adj(bell)(a, b)
- Parameters:
gate – Quantum gate to apply.
*arg – Qubits or classical parameters for the gate.
ket_process – Explicitly specify the quantum process. If not provided, the process is inferred from the qubits.
**kwargs – Additional parameters for the quantum gate.
- cat(*gates) Callable[[Any], Any] ¶
Concatenate gates.
Create a new callable concatenating all the quantum gates.
Example
from ket import * z_gate = cat(H, X, H) p = process() q = p.alloc() z_gate(q)
- Parameters:
*gates – Quantum gates to concatenate.
- Returns:
A new callable that concatenates the given quantum gates.
- control(control_qubits: Quant)¶
Controlled scope.
Opens a controlled scope where quantum operations are applied only if the qubits of the parameter
control_qubits
are in the state \(\ket{1}\).- Usage:
with control(control_qubits): # Apply operations only if control_qubits are in the state |1> ...
Example
from ket import * p = Process() c = p.alloc(2) a, b = q.alloc(2) # CNOT c[0] a with control(c[0]): X(a) # Toffoli c[0] c[1] a with control(c): X(a) # CSWAP c[0] a b with control(c[0]): SWAP(a, b)
- Parameters:
control_qubits – The qubits to control the quantum operations.
- ctrl(control_qubits: Quant, gate: Callable) Callable ¶
Add control qubits to a gate.
Create a new callable that applies the given
gate
with the control qubits.- Usage:
from ket import * p = Process() c = p.alloc(2) a, b = q.alloc(2) # CNOT c[0] a ctrl(c[0], X)(a) # Toffoli c[0] c[1] a ctrl(c, X)(a) # CSWAP c[0] a b ctrl(c[0], SWAP)(a, b)
- Parameters:
control_qubits – The qubits to control the quantum operations.
gate – The gate to apply with the control qubits.
- Returns:
A new callable that applies the given gate with the control qubits.
- dump(qubits: Quant) QuantumState ¶
Obtain the quantum state snapshot.
- Parameters:
qubits – Qubits to be observed.
- Returns:
Object representing the quantum state.
- exp_value(hamiltonian: Hamiltonian | Pauli) ExpValue ¶
Calculate the expected value for a quantum state.
- Parameters:
hamiltonian – Hamiltonian or Pauli operator for calculating the expected value.
- Returns:
Object representing the expected value.
- inverse(process: Process)¶
Inverse scope.
Open a scope where all the quantum operations are executed backwards.
- Usage:
with inverse(process): # Classical and quantum operations ...
- Parameters:
process – The process where the inverse operations will be applied.
- is_diagonal(gate: Callable) Callable ¶
Force to consider as a diagonal gate.
- Parameters:
gate – Quantum gate
- is_permutation(gate: Callable) Callable ¶
Force to consider as a permutation gate.
- Parameters:
gate – Quantum gate
- kron(*gates, n: int = 1) Callable[[Any], Any] ¶
Gates tensor product.
Create a new callable that with the tensor product of the given gates.
Example
from ket import * p = process() a, b = p.alloc(2) HX = kron(H, X) HX(a, b) # Apply an Hadamard on qubit `a` and a Pauli X on qubit `b`
- Parameters:
*gates – Quantum gates to tensor product.
- Returns:
A new callable that represents the tensor product of the given quantum gates.
- measure(qubits: Quant) Measurement ¶
Measure the given qubits and return a measurement object.
- Parameters:
qubits – Qubits to be measured.
- Returns:
Object representing the measurement results.
- sample(qubits: Quant, shots: int = 2048) Samples ¶
Get the quantum state measurement samples.
- Parameters:
qubits – Qubits to be measured.
shots – Number of measurement shots.
- Returns:
Object representing the measurement samples.
- using_aux(unsafe: bool = False, **names)¶
Add axillary qubits to a quantum gate.
Example
@using_aux(a=lambda c: 0 if len(c) <= 2 else 1) def v_chain(c, t, a): if len(c) <= 2: ctrl(c, X)(t) else: with around(ctrl(c[:2], X), a): v_chain(a + c[2:], t)
- Parameters:
unsafe – Use dirty unsafe qubits. Defaults to False.