uniqc.algorithmics.measurement.pauli_expectation module#
Pauli string expectation value measurement via basis rotation.
- uniqc.algorithmics.measurement.pauli_expectation.pauli_expectation(circuit, pauli_string, shots=None)[source]#
Measure the expectation value of a Pauli string on a circuit.
For each qubit i, the measurement basis is determined by
pauli_string[i]:'I': trace out (identity, contributes trivially)'Z': measure in the computational (Z) basis — no rotation needed'X': apply Hadamard before Z measurement'Y': apply Sdag then Hadamard before Z measurement
When
shotsisNone, the statevector simulator is used to compute the exact expectation analytically. Whenshotsis given, the circuit is simulatedshotstimes and the empirical frequency is used.- Parameters:
circuit (Circuit) – Quantum circuit. Must contain only gates supported by
QASM_Simulatorand end with measurement instructions.pauli_string (str) – Case-insensitive Pauli string (e.g.
"XYZ","IZI"). Characters must beI,X,Y, orZ.shots (int | None) – Number of measurement shots.
Noneuses statevector mode for the exact analytical value.
- Returns:
Expectation value ⟨psi|P|psi⟩ as a float in the interval
[-1, 1].- Raises:
ValueError –
pauli_stringcontains invalid characters or its length does not match the number of qubits incircuit.ValueError –
shotsis not a positive integer.
- Return type:
Example
>>> from uniqc.circuit_builder import Circuit >>> from uniqc.algorithmics.measurement import pauli_expectation >>> c = Circuit() >>> c.h(0) >>> c.cx(0, 1) # Bell state (|00⟩+|11⟩)/√2 >>> c.measure(0, 1) >>> pauli_expectation(c, "ZZ", shots=None) # exact: 1.0 1.0 >>> abs(pauli_expectation(c, "ZZ", shots=10000) - 1.0) < 0.1 True