Problem Statement
Story
We are the QCoder Order, worshippers of quantum programming. We have a hidden polynomial, , that has been protected for centuries. It is defined using an -bit odd integer and an -bit integer as follows:
Recently, we performed a sacred ritual to transfer this polynomial to an oracle . If the ritual was successful, the oracle should behave as follows:
Here, denotes the bitwise XOR operator, and denotes the remainder when is divided by .
Unfortunately, because the ritual's procedure is extremely complicated, no one knows if the oracle was generated correctly. We know that if the ritual failed, the oracle would acquire an extra phase and behave as follows:
Here, denotes the quotient when is divided by .
Your mission is to determine whether the ritual succeeded—i.e., whether the oracle has been implemented correctly.
Note that the polynomial is our top secret. To protect the details of , you can only call the oracle once.
Detailed Description
Given an integer , you may implement any operations you like on a -qubit quantum circuit , and on a quantum circuit that has qubits and one classical register.
Implement operations on these quantum circuits that satisfy the following requirement.
The candidate oracles and act on basis states , where and , as follows:
When are applied in this order to the zero state , the measurement result written to the classical register must be if , and if .
Constraints
- is odd.
- Integers must be encoded by little-endian.
- Global phase is ignored in judge.
- The submitted code must follow the specified format:
before_oracle(n: int) -> QuantumCircuit: the quantum circuit before calling the oracle.after_oracle(n: int) -> QuantumCircuit: the quantum circuit after calling the oracle. You must write the measurement result to the classical registercin this circuit.
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
"""
You can apply measurement as follows
(e.g., when measuring x[0]):
qc.measure(x[0], c[0])
"""
def before_oracle(n: int) -> QuantumCircuit:
x = QuantumRegister(n)
y = QuantumRegister(n)
qc = QuantumCircuit(x, y)
# Write your code here:
return qc
def after_oracle(n: int) -> QuantumCircuit:
x = QuantumRegister(n)
y = QuantumRegister(n)
c = ClassicalRegister(1)
qc = QuantumCircuit(x, y, c)
# Write your code here:
return qcIn the judge, the functions are called in the following order:
before_oracle(n)- (the oracle )
after_oracle(n)
After that, the judge concludes that “the ritual succeeded” if , and that “the ritual failed” if .
Hints
Open
- You can use the following code to test your solution in your local environment.
- Note that you need to define
before_oracle,oracle, andafter_oraclein advance.
- Note that you need to define
from qiskit_aer import AerSimulator
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile
# Define or import before_oracle / oracle / after_oracle here.
# Example input
n: int = 3
A: int = 1 # A is odd
B: int = 4
# Build the circuit
x = QuantumRegister(n)
y = QuantumRegister(n)
c = ClassicalRegister(1)
main_qc = QuantumCircuit(x, y, c)
main_qc.compose(before_oracle(n), inplace=True)
main_qc.compose(oracle(), inplace=True)
main_qc.compose(after_oracle(n), inplace=True)
# Simulate the circuit and retrieve the contents of register c
simulator = AerSimulator()
job = simulator.run(
transpile(main_qc, simulator),
shots=1024,
)
counts: dict[str, int] = job.result().get_counts()
print(f"Measurement statistics: {counts}")