Problem 352018 · medium · Quantum Computing · Level 03 Entanglement

Rewiring the Register

qubit permutation · SWAP gate · bit manipulation · state-vector simulation

A register of n qubits is in the state state, a list of 2^n amplitudes. A chip's wiring changes, and the qubits get new numbers: new qubit j is the qubit that used to be qubit order[j]. order is a permutation of 0, 1, ..., n-1. The physical state does not change; only the labels do, so every amplitude moves to a new index. Exchanging two qubits this way is what the SWAP gate does.

Precisely: the new amplitude of basis state |c_0 c_1 \dots c_{n-1}\rangle is the old amplitude of the basis state |b_0 b_1 \dots b_{n-1}\rangle with b_{\text{order}[j]} = c_j for every j. For a product state this turns kron(q0, q1, ..., q_{n-1}) into kron(q_order[0], q_order[1], ...).

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 reorder_qubits(state, order) that returns the new state (a list or a numpy array). Any global phase is accepted: [1j, 0] is the same state as [1, 0].

The larger tests use two helpers that you can also call: test_state(n, seed) returns a normalised state of n qubits, and peek(state) returns 8 of its amplitudes, [state[(j * 40503) % len(state)] for j in range(1, 9)].

Examples

Input:  state = [0, 1, 0, 0], order = [1, 0]
Output: [0, 0, 1, 0]
Explanation: the two qubits exchange labels: |01> becomes |10>.

Input:  state = [0, 0, 0, 1, 0, 0, 0, 0], order = [2, 0, 1]
Output: [0, 0, 0, 0, 0, 1, 0, 0]
Explanation: the old state is |011> (b0=0, b1=1, b2=1). New qubit 0 is old qubit 2 (reads 1),
new qubit 1 is old qubit 0 (reads 0), new qubit 2 is old qubit 1 (reads 1): |101>, index 5.

Input:  state = [0.6, 0, 0.8, 0], order = [1, 0]
Output: [0.6, 0.8, 0, 0]
Explanation: kron([0.6, 0.8], [1, 0]) becomes kron([1, 0], [0.6, 0.8]).

Constraints

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

Goals

  • Renumber the qubits of a register by moving every amplitude to its new index
  • Read a permutation in the stated direction: new qubit j is old qubit order[j]
  • See SWAP as the special case of exchanging two qubits
Starting Python…