CF 1357C2 - Prepare superposition of basis states with the same parity
The problem asks us to prepare a quantum state over $N$ qubits where only the basis states with a specific parity of ones are included in an equal superposition.
CF 1357C2 - Prepare superposition of basis states with the same parity
Rating: -
Tags: *special
Solve time: 1m 45s
Verified: yes
Solution
Problem Understanding
The problem asks us to prepare a quantum state over $N$ qubits where only the basis states with a specific parity of ones are included in an equal superposition. Here, parity 0 means we include all bitstrings with an even number of ones, and parity 1 means all bitstrings with an odd number of ones. We start with all qubits initialized to $|0\rangle$ and are restricted to using the Pauli gates (X, Y, Z), the Hadamard gate, and controlled versions of these gates. Measurement is allowed, but the goal is to prepare the state deterministically.
The input is simply $N$, the number of qubits, and the desired parity. The output is the final state of the qubits, which cannot be represented as a number but rather must be prepared physically in the simulator or device. This problem does not involve classical output - correctness is defined by the quantum state being exactly the uniform superposition over the correct subset of basis states.
The constraints allow up to around $N = 30$ qubits for practical simulations, which means any brute-force approach iterating over $2^N$ states would be infeasible. The key is to manipulate the qubits with gates to prepare the superposition directly without enumerating the exponential number of states. Edge cases include $N = 1$, where parity directly determines whether we flip the qubit or not, and small values like $N = 2$, which show how the parity condition partitions the basis states.
Approaches
The naive approach would be to try and construct the superposition by applying Hadamard gates to every qubit to create the uniform superposition over all $2^N$ states and then somehow remove the states that have the wrong parity. This is theoretically possible by using multi-controlled Z gates to invert amplitudes conditionally, but it becomes cumbersome and requires a depth of controlled operations proportional to $N$. This approach is conceptually correct but practically inefficient.
The key insight is to observe that a uniform superposition over all states can be adjusted to have definite parity by flipping a single qubit conditionally. If we apply Hadamard gates to $N-1$ qubits, we generate a superposition over all $2^{N-1}$ bitstrings for those qubits. The parity of the entire $N$-qubit system can then be set by flipping the last qubit depending on the parity of the first $N-1$ qubits. This uses the controlled-X gate (or CNOT) with $N-1$ controls and guarantees the final parity is correct. It reduces the problem to a linear sequence of gate applications and a single multi-controlled operation, avoiding enumeration of all $2^N$ states.
| Approach | Time Complexity | Space Complexity | Verdict |
|---|---|---|---|
| Brute Force | O(2^N) | O(2^N) | Too slow |
| Optimal | O(N) | O(1) | Accepted |
Algorithm Walkthrough
- Apply the Hadamard gate to the first $N-1$ qubits. This creates a uniform superposition over all bitstrings of length $N-1$. This is correct because Hadamard on $|0\rangle$ produces $(|0\rangle + |1\rangle)/\sqrt{2}$, and the tensor product over $N-1$ qubits gives all combinations equally.
- Apply a multi-controlled X gate targeting the last qubit, controlled on the first $N-1$ qubits. The control condition is such that the last qubit flips only when the first $N-1$ qubits have a parity that requires the final qubit to reach the desired overall parity. If the parity parameter is 0, flip the last qubit when the first $N-1$ qubits have odd parity; if parity is 1, flip it when the first $N-1$ qubits have even parity. This guarantees that every resulting basis state has the correct parity.
- The final quantum state is now the uniform superposition over all basis states with the requested parity. No further gates or measurements are needed unless the parity needs verification.
Why it works: The algorithm works because any $N$-bit string can be uniquely decomposed into the first $N-1$ bits and the last bit. By preparing all $2^{N-1}$ combinations for the first $N-1$ qubits and then deterministically setting the last qubit to satisfy the parity requirement, we guarantee that exactly all desired states are included and no undesired state is present. The Hadamard gates create uniform amplitudes, and the multi-controlled X gate flips the final qubit conditionally without affecting amplitudes.
Python Solution
Quantum solutions are not directly implemented in Python for Codeforces, but we can write a Q#-style simulation for understanding:
from qiskit import QuantumCircuit
def prepare_parity_superposition(N, parity):
qc = QuantumCircuit(N)
# Apply Hadamard to first N-1 qubits
for i in range(N-1):
qc.h(i)
# Compute parity of first N-1 qubits and flip last qubit conditionally
controls = list(range(N-1))
if parity == 0:
qc.mcx(controls, N-1) # flip if first N-1 qubits have odd parity
else:
# For odd parity, flip last qubit if first N-1 qubits have even parity
qc.x(N-1)
qc.mcx(controls, N-1)
qc.x(N-1)
return qc
# Example usage for N=2, parity=0
qc = prepare_parity_superposition(2, 0)
print(qc.draw())
This code mirrors the algorithm. The loop applies Hadamards to the first $N-1$ qubits. The mcx gate represents a multi-controlled X gate. For parity 1, the final qubit is flipped before and after the mcx to achieve the opposite control condition.
Worked Examples
Example 1: N=2, parity=0
| Step | Qubit 0 | Qubit 1 | Description |
|---|---|---|---|
| Initial | 0 | 0 | All qubits in |
| Hadamard on Q0 | superposition | 0 | Q0 in ( |
| MCX (control Q0) | superposition | Q1 flipped if Q0=1 | Q1 flips when Q0=1 to make parity even |
Resulting state: (|00> + |11>)/√2
Example 2: N=3, parity=1
| Step | Q0 | Q1 | Q2 | Description |
|---|---|---|---|---|
| Initial | 0 | 0 | 0 | All zeros |
| Hadamard on Q0,Q1 | superposition | superposition | 0 | First 2 qubits in uniform superposition |
| MCX (control Q0,Q1) with parity adjustment | superposition | superposition | Q2 flips to ensure total parity odd | All states with odd ones included |
This trace shows the algorithm deterministically sets the last qubit to ensure the required parity across all superposition components.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(N) | We apply N-1 Hadamard gates and one multi-controlled X gate |
| Space | O(1) | No extra storage proportional to N is needed beyond the qubits themselves |
The linear gate count fits comfortably within practical constraints even for $N$ around 30.
Test Cases
# These are illustrative for quantum circuits
qc = prepare_parity_superposition(1, 0) # |0>
qc = prepare_parity_superposition(1, 1) # |1>
qc = prepare_parity_superposition(2, 0) # (|00> + |11>)/√2
qc = prepare_parity_superposition(2, 1) # (|01> + |10>)/√2
qc = prepare_parity_superposition(3, 0) # even parity states of 3 qubits
qc = prepare_parity_superposition(3, 1) # odd parity states of 3 qubits
| Test input | Expected output | What it validates |
|---|---|---|
| N=1, parity=0 | 0> | |
| N=1, parity=1 | 1> | |
| N=2, parity=0 | ( | 00>+ |
| N=2, parity=1 | ( | 01>+ |
| N=3, parity=1 | all 3-qubit odd parity states | Multi-controlled X correctness |
Edge Cases
For N=1, parity=0, the Hadamard loop does nothing and the MCX is trivial; the algorithm returns |0> as expected. For N=1, parity=1, the algorithm flips the last qubit to |1>, correctly handling the single-qubit case. For larger N, the algorithm systematically enforces parity using only the last qubit, avoiding off-by-one errors in multi-qubit parity calculation. This