Problem 388679 · medium · Quantum Computing · Level 03 Entanglement

Reading One Qubit, Keeping the Rest

partial measurement · Born rule · state collapse · normalisation

A register of n qubits is in the state state, a list of 2^n complex amplitudes with \sum_i |a_i|^2=1. Measuring only qubit k gives outcome (0 or 1) with probability p, the total of |a_i|^2 over the basis states i in which qubit k reads outcome. After the measurement the register is still n qubits, in the collapsed state: every amplitude whose basis state has qubit k reading the other value becomes 0, and every remaining amplitude is divided by \sqrt p so that the state is normalised again.

Bit order: basis state |b_0 b_1 \dots b_{n-1}\rangle has index int("b0b1...", 2), so qubit 0 is the leftmost bit, the most significant. In 3 qubits, |011\rangle is index 3.

Write measure_qubit(state, k, outcome) that returns the pair (p, collapsed). If p < 10^{-12} the outcome cannot happen and there is nothing to collapse to: return (0.0, None). The probability is compared as a number; any global phase is accepted in the collapsed state: [1j, 0] is the same state as [1, 0].

test_state(n, seed) is a helper that returns a normalised state of n qubits; some tests use it.

Examples

Input:  state = [0.5, 0.5, 0.7071067811865476j, 0], k = 1, outcome = 1
Output: (0.25, [0, 1, 0, 0])
Explanation: qubit 1 reads 1 in |01> and |11> (indices 1 and 3): p = 0.25 + 0 = 0.25,
and the amplitude 0.5 of index 1 becomes 0.5 / sqrt(0.25) = 1.

Input:  state = [0.5, 0.5, 0.7071067811865476j, 0], k = 0, outcome = 0
Output: (0.5, [0.7071067811865475, 0.7071067811865475, 0, 0])

Input:  state = [1, 0, 0, 0], k = 0, outcome = 1
Output: (0.0, None)

Constraints

  • the register has 1 to 10 qubits
  • numbers are compared with a tolerance of 1e-6

Goals

  • Find the probability of one qubit's outcome in a register
  • Collapse the register: keep the consistent amplitudes and renormalise
  • Handle an outcome that cannot happen
Starting Python…