We are running this on Qiskit v0.43
Go to Anaconda Powershell and run these commands to install necessary libraries
conda create -n qiskit043 python=3.9
conda activate qiskit043
pip install qiskit==0.43
KenKen is a logic-based numerical puzzle that combines elements of Sudoku and arithmetic. The puzzle consists of a grid. The grid is divided into "cages," each with a target number and a mathematical operation (addition, subtraction, multiplication, division, and sometimes single cell cages with no operation). The goal is to fill the grid with numbers while ensuring that:
- Each row and column contains unique numbers.
- The numbers within each cage satisfy the given mathematical operation to reach the target number.
A quantum computer is a super cool kind of computer that uses qubits. Unlike regular bits that are either 0 or 1, qubits can be 0 and 1 at the same time (called superposition). This lets us try out many possibilities all at once — a bit like solving every possible answer in parallel.
Grover's algorithm is a quantum search algorithm that finds a specific item in an unsorted database faster than classical methods. It achieves this by amplifying the probability of the correct answer through repeated quantum operations, reducing the number of searches needed from N to √N.
A classical solution could be trying out all possible combinations and checking if they work, resulting in O(N!^(N)*(N²)) time complexity. However, since we use Grover's algorithm there is a quadratic speedup, and our new time complexity is O(√(N!^(N)*(N²))).
First, we make each KenKen square into a qubit. Since the grid is 2x2, we have 4 qubits, and we can say that:
0means there is a 1 in that square.1means that there is a 2 in that square.
(Since there can only be 2 possible numbers in a 2x2 KenKen.)
We apply a Hadamard (H) gate to all the qubits and prepare the ancilla flag qubit.
We have to check:
- If the rows and columns are different
- If the boxes (cages) are satisfied
There are two parts to the oracle: the row/column part and the box part.
We check if:
((q0 xor q1) & (q2 xor q3)) & ((q0 xor q2) & (q1 xor q3))
We can create an ancilla qubit at 0:
CNOT(q1, ancilla)CNOT(q2, ancilla)
If ancilla is 0 → they’re the same; if 1 → they’re different.
Create another ancilla qubit at 0:
- Use a Toffoli (CCX) gate:
- Controls: a1 and a2
- Target: a3
- If a3 is 1 → both a1 and a2 were 1
Ancilla bits: a1 to a7, all initialized to 0.
CNOT(q0, a1)
CNOT(q1, a1)
CNOT(q2, a2)
CNOT(q3, a2)
CNOT(q0, a3)
CNOT(q2, a3)
CNOT(q1, a4)
CNOT(q3, a4)
CCX(a1, a2, a5)
CCX(a3, a4, a6)
CCX(a5, a6, a7)
If a7 is 1, then the rows and columns are all different, otherwise they’re not. Our target solution will have it as 1.
- Addition: Use ripple carry adder, then XOR to compare to target.
- Subtraction: Convert to 2’s complement, then add and XOR to compare.
- Multiplication: Use shift-and-add multiplier, then XOR to compare.
- Division: If
b / s = t, thents = b→ use multiplication.
Since all subtraction and division constraints in a 2x2 grid have 2 cells and must be 1:
- Just check if they’re different
This saves memory.
boxes = [
(3, '+', [0, 1]),
(1, '', [3]),
(2, '', [2]),
]- Format:
(target, operation, [cells]) - Cells:
0 = top-left,1 = top-right,2 = bottom-left,3 = bottom-right
Use AND gates to combine:
- All box constraints
a7from rows/columns
Let the final ancilla qubit comparing all constraints be a_final, then:
qc.z(a_final)To amplify the marked states:
- Apply Hadamard to all qubits
- Apply X gates
- Apply MCX (multi-controlled X) gate
- Undo the X and H gates
This amplifies the probability of correct states.
- Measure the data qubits (not ancilla).
- Interpret the bitstrings and print the solution.
We don't have enough memory to run the Grover loop multiple times.
If we could, our code would be more accurate. Right now, because we have one iteration of Grover's algorithm, and since we have 16 states, the probability of getting the correct state is 47%. To make it get 95-99 percent accuracy, we would have to have at least 3 Grover iterations for a 2x2 Ken Ken. A possible solution is to create more short cuts for addition and multiplication as well abusing the fact that the Ken Ken is 2x2.