# Securinets NCSC IV CTF - Quantum Challenges Writeup

# QASM Writeup

## Description:

Can you make this quantum circuit work? Flag Format: Securinets{output}

Author: mida0ui

Attachment: circuit.qasm

The file “circuit.qasm” was attached with the task, you can find it in this repo.

First of all, what is QASM?

QASM(quantum assembly language) is a simple text-format language for describing acyclic quantum circuits composed from single qubit, multiply controlled single-qubit gates, multiple-qubit, and multiple-qubit controlled multiple-qubit gates.

A QASM program declares the classical bits and qubits, describes the operations (gates) on those qubits and the measurements needed to obtain the classical result by inspecting the qubits.

In this challenge, the only thing that needs to be done is to open the qasm file and run the circuit describes.

This can be done in various ways, there’s no single intended solution, i will only state 2 solutions in this writeup.

### Solution 1:

You can import the code in the QASM file in IBM Quantum composer: https://quantum-computing.ibm.com/composer/files/new

Yhe output of this circuit is always 01001 (with a 100% probability).

Flag: Securinets{01001}

### Solution 2:

You can import the QASM file code and run the circuit using qiskit.

In this case, to make the quantum circuit work, you only have to add measurements.

1 | from qiskit import * |

The reuslt of this code is:

But the circuit contains a quantum register of 5 qubits, we have to get the measurement result of them all: we notice that all the quantum gates described in the QASM file did not change the state of the fifth qubit, so its still in the default value.

The default state of a qubit, if not initialisaed is |0>.

So the overall output of the circuit is 01001 (always, with a 100% probability).

Flag: Securinets{01001}

# Secret Message 1

## Description

Oh look! I managed to get this super secret message, but i cannot decode it :( The message is the output of this circuit.

The message is: òßÒïá×ÙßìâæÔ,í#ÙÞúÝïÙúî#.Ôúãï,Ù.ïÛúÎ,ì”/*ú#Ñ!Ó-Üë

Can you help me ?

Author: mida0ui

## Solution

A quantum gate is basic quantum circuit operating on a small number of qubits, they perform operations to change the state of the qubits, and unlike many classical logic gates, quantum logic gates are reversible.

Each quantum gate has its matrix, and since the qubit states are represented with vectors, the gates transform these vectors to get new states.

The state vectors of the states |0> and |1> are:

First of all, lets explain the components of the given quantum circuit:

#### The X-Gate:

A single-qubit gate represented by the Pauli-X matrix:

the X-gate simply switches the amplitudes of the states |0> and |1>, you can see it like this: A qubit has a probability of measuring zero and a probability of measuring one, and an X gate swaps those probabilities. It perfroms a π rotation around the x-axis.

#### The Y-Gate:

Similar to the X gate, it’s single-qubit gate represented by the Pauli-Y matrix:

It performs a π rotation around the y-axis in the Bloch sphere.

#### The S-Gate:

The Phase gate (or S gate) is a single-qubit operation represented by the matrix:

The S gate is also known as the phase gate or the Z90 gate, because it represents a 90-degree rotation around the z-axis.

#### The Controlled Not or CX Gate:

The CNOT gate iss a two-qubit quantum gate that performs a controlled NOT where one qubit is the control and the second qubit is the target. Its matrix is the following :

When applied:

The mathematics behind these results are simple:

Lets take |10> and |11>:

This ⊗ represents a tensor product, you can read about it here if you don’t know what it is : https://www.math3ma.com/blog/the-tensor-product-demystified

The first qubit is usually referred to as the control qubit and the second qubit as the target qubit. Expressed in basis states, the CNOT gate: leaves the control qubit unchanged and performs a Pauli-X gate or NOT gate (which just flips the state of the qubit) on the target qubit when the control qubit is in state ∣1⟩; leaves the target qubit unchanged when the control qubit is in state ∣0⟩.

#### The SWAP Gate:

The SWAP gate is two-qubit operation. Expressed in basis states, the SWAP gate swaps the state of the two qubits involved in the operation:

#### Reversing the given circuit:

All the matrices of the gates stated bellow (and in genereal in quantum computing) are reversible, which means, the gate can be applied to reverse its own effet, so the only thing we have to do is to put them in a reversed order, and we get our flag.

We transform the output given in the description into a binary string to fit in the quantum circuit, the encoding used here is ‘windows-1252’ because the output string uses characters from the extended ASCII table, so we cannot use utf-8 for example. We create a quantum circuit with one quantum register containing 2 qubits and one classical register to measure the state of those qubits, put the gates in a reversed order and pass our string in a loop and decode the message slowly.

1 | from qiskit import * |

Flag: Securinets{h4v1ng_fun_w17h_qu4n7um_G4t35?_1b2a6d}

# Secret Message 2

## Description

I want to tell you a secret, but i’m afraid someone will intercept our communication. I know how good you are now with quantum encoding, so I will let you figure it out on your own.

I used the following code to get to encode the secret:

1

2

3

4

5 qc.s(q[2])

qc.ccx(q[0],q[1],q[2])

qc.h(q[2])

qc.swap(q[1],q[0])

qc.y(q[0])

The secret is: VMëÜÁÂÎèÜAlso, the secret contains two english words, it doesn’t contain special characters nor numbers.

Flag format: Securinets{decoded_message}

BE CAREFUL, YOU ONLY HAVE 5 attempts

Author: mida0ui

## Solution

From the description of this task, we can see that the CCNOT gate was used, along with other gates to encode the message, meaning that we need to use a quantum register containing 3 qubits to solve this task. The CCNOT gate is similar to the CNOT gate seen in the previous task, but it’s a three-qubit gate, it is applied to one target qubit based on the state of two control qubits:

To encode the message, a very special gate waas used too, the Hadamard gate.

The Hadamard gate is a single-qubit operation that maps the basis state ∣0⟩ to = |+> and |1> to = |->, thus creating an equal superposition of the two basis states.

It has the matrix:

A qubit can exist in a superposition of its two “basis” states, which loosely means that it is in both states simultaneously. When measuring a qubit, the result is a probabilistic output of a classical bit, so for one single qubit, the measurement can give either |1> or |0> depending on the probabilistic state.

With that said, we cannot use one iteration to decode the message like in the previous task, we need to print all the possibilities (or almost) to see the different reults of measurements, and get the decoded message.

To make the task easier and less time consuming, the description specifies that the secret contains two english words and it doesn’t contain special characters nor numbers. So we will filter the results based on these two facts.

1 | import matplotlib |

Flag: Securinets{youwin}

**Title:**Securinets NCSC IV CTF - Quantum Challenges Writeup**Author:**admida0ui**Created at:**2023-03-22 20:00:00-
**Updated at:**2023-08-09 12:12:51 -
**Link:**https://admida0ui.tech/2023/03/22/ncsc-iv-quantum/ -
**License:**This work is licensed under CC BY-NC-SA 4.0.