Solving a Secret Santa SAT Problem with Classiq
Welcome to my third blog in this series on how to solve a Secret Santa problem using quantum computers.
This year I want to show how easy it is to generate Q# code to solve the problem using the Classiq platform.
This blog is part of the Q# Holiday Calendar 2022.
Let’s first see what the problem was.
We have 3 or more people who want to play Secret Santa. They all put their name on a piece of paper, and all the papers go into a bowl. After a shuffle, each player will pick a piece of paper, and given that no one has their own name, each player has a name for which he has to buy a present and/or write a poem.
This can be easily visualized using this table, in this case we have 3 players.
In this case if X1 = 1, Vincent has to buy Shai a present.
To satisfy this problem you must have exactly one value in each row and each column. We can write this as a SAT problem like this:
For a more detailed description check my first blog post here.
Bring in Classiq
Classiq allows end users to generate efficient logical quantum circuits from a high level language. We can use the Classiq platform to generate a circuit which can solve this challenge, without thinking about qubits or low level gate based programing. We can use either the Python SDK, which we will use below, or a textual model to create these logical quantum circuits.
Let’s see how we can do this.
Firstly let's create the SAT formula as a Python string:
Next, let’s create an Arithmetic Oracle to solve the SAT problem:
What we see here is that we first set the register size to 1, this means that each variable can only take the value 1 or 0. Next we load the formula into the ArithmeticOracle.
To actually create a circuit from this, we will pass this oracle to Classiq's synthesis engine like this:
Multiple things are happening here so let’s walk though each:
- First, we define an optimization parameter. This will tell the synthesis engine to create the circuit which uses the least amount of qubits.
- Secondly, we pass the specification of the backend to the synthesis engine. This will make sure we do not use more qubits than the device has. The device we have selected here is the Ionq.qpu device in Azure Quantum. We will also optimize the circuit taking into account the native supported gate set and the QPU topology. Here we also specify the output format, saying that we want to get Q# code as output.
- Next, we add the oracle to our ModelDesiger instance.
- Lastly, we create the circuit given the functionality, the oracle, and the constraints/preferences.
Let’s have a look at the Oracle after running the synthesis engine that is generated below or here.
Now that we have the oracle, we also want to have a look at the Q# code for this oracle.
With 'circuit.qsharp' we can get the Q# code that is generated, available here.
Now that we have everything in place it is time to execute the program which can be done like this:
Now you are curious who has to give who a present, so here is the output which can be found with 'res.result':
Which means Vincent will get Simon a present, Shai will get Vincent a present and lastly, Simon will get Shai a present.
This shows how easy real-world, functional Q# code can be generated and executed using the Classiq platform.