Hacky Holidays: Quantum Snacks Writeup

Updated on 2021-08-16


I recently participated in the Hacky Holidays CTF and came across a very interesting challenge that revolved around quantum computing, we are given qbits and 3 logic gates, qbits in this scenario are represented as vectors, we are also provided with the a formula to calculate the change in the state of a qbit upon interaction with a qbit, more detailed information can be found below,

Challenge Explanation

Flag 1

Considering only the given 3 logic gates and starting in the (1 0) state, how many states can the qubit have?

This is relatively simple, using the logic gates and the given qbit we can calculate that there are 8 possible states.

Flag 2

Provide a circuit (a series of operations) that transforms the (1 0) state to the (-1 0) state, only using the H and X operations.

This is where the challenge gets interesting, to solve this question I had to begin representing the problem programmatically, I had chosen python to do so.


Above I defined the logic gates provided and the square root number, the code assumes the qbits to be vectors and gates to be matrices. I have also defined two functions, one to multiply vectors with matrices and the other that compares a starting and ending state, it randomly passes the qbit through a gate until both the states are the same.

Main Function

using the defined functions we multiply the vectors with a randomly picked gate until we get our desired output, as shown below,

Solution 2

Flag 3

Same as question 2 but now provide the shortest circuit possible (minimal number of gates). You are allowed to use H, X and Z.

Using our programmatic implementation this was easy to implement as we now have access to all gates and can obtain our result by setting a condition for the output.

In the code since we cannot directly find out the smallest number of gates possible we must manually increase the number of gates we set as the minimum in the condition.

Solution 3


I found this CTF very cool and interesting, personally its not something I’ve seen very frequently. The entire program I used to solve the challenge can be found here

I hope you found the challenge as interesting as I did!