Expected value calculation utilities.

This module provides base classes creating a Hamiltonian fro expected value calculation.

Classes ket.expv


Expected value for a quantum state.


Hamiltonian for expected value calculation.


Pauli operator for Hamiltonian creation.

class ExpValue(hamiltonian: Hamiltonian | Pauli)

Expected value for a quantum state.

This class holds a reference for a expected value result. The result may not be available right after the measurement call, especially in batch execution.

To read the value, access the attribute value. If the value is not available, the measurement will return None; otherwise, it will return a float.

You can instantiate this class by calling the exp_value function.


from ket import *

p = Process()
q = p.alloc(2)
CNOT(H(q[0]), q[1])
result = exp_value(Pauli("X", q))
print(result.value) # 1.0000000000000004
property value: float | None

Retrieve the expected value if available.

get() dict[int, int]

Retrieve the expected values.

If the value is not available, the quantum process will execute to get the result.

class Hamiltonian(pauli_products: list[Pauli], process: Process)

Hamiltonian for expected value calculation.

This class is not intended to be instantiated directly. Instead, it should be created by adding Pauli operators or other Hamiltonian objects.

class Pauli(pauli: Literal['X', 'Y', 'Z'], qubits: Quant, *, _process: Process | None = None, _pauli_list: list[str] | None = None, _qubits_list: list[Quant] | None = None, _coef: float | None = None)

Pauli operator for Hamiltonian creation.

This class represents a Pauli operator for Hamiltonian creation. The primary usage of this class is to prepare a Hamiltonian by adding and multiplying Pauli operators and scalars for calculating the expected value of a quantum state.

Allowed operations: - Multiply by a scalar or another Pauli operator. - Add another Pauli or Hamiltonian operator.


from ket import *

p = Process()
qubits = p.alloc(3)

# Example 1: Single Pauli term
pauli_term = Pauli("X", qubits[0])
# <Ket 'Pauli' 1.0*X0, pid=0x...>

# Example 2: Sum of Pauli terms
sum_pauli = Pauli("Y", qubits[1]) + Pauli("Z", qubits[2])
# <Ket 'Hamiltonian' 1.0*Y1 + 1.0*Z2, pid=0x...>

# Example 3: Pauli multiplication
multiplied_pauli = 2.0 * Pauli("X", qubits[0]) * Pauli("Y", qubits[1])
# <Ket 'Pauli' 2.0*X0Y1, pid=0x...>

# Example 4: Calculating expected value
h = Pauli("Z", qubits[0]) + Pauli("X", qubits[1]) + Pauli("Y", qubits[2])
# <Ket 'Hamiltonian' 1.0*Z0 + 1.0*X1 + 1.0*Y2, pid=0x...>
result = exp_value(h)
print(result.value)  # 3.0000000000000013
  • pauli – Pauli operator type.

  • qubits – Qubits to apply the Pauli operator to.

  • processFor internal usage. Quantum process, default is the process of the given qubits.

  • pauli_listFor internal usage. List of Pauli operators.

  • qubits_listFor internal usage. List of Qubit.

  • coefFor internal usage. Coefficient for the Pauli operator, default is 1.0.