# Introduction

Four challenges to complete in four days - IBM sure knows how to tug at my heartstrings. Initially unaware of the challenge it was only ironically as I was preparing to deliver a Career Week Quantum Computing presentation with a colleague did IBM's Quantum Challenge come up in conversation. A quick look at the introductory page and I put on an extra pot of coffee as I knew I would be making up for loss time of the first two days.

The challenges were a perfect difficulty level for my current level of QC knowledge. Challenges 1 and 2 were introductory and helped get back in the groove of working with Qiskit, adding an element of error correction that I was not familiar with previously. Challenge 3 provided an opportunity to work with Quantum Cryptography, specifically a portion of BB84. I was already familiar with BB84 theory and this was more of a challenge on how to get it implemented practically. Lastly, challenge 4 is where we jumped off the deep end. Not only were the concepts involved things I had not heard of yet, the Qiskit specific functionality required to proceed further was also foreign to me at the start. This challenge pushed my initial boundaries and I'm happy to say with some determination and a lot of reading I was able to complete it with my own inneficient approach.

Your *"final score"* being determined by how efficient your solution was also helped drive the community to not only solve the challenge, but push themselves to improve their solutions further. I took the opportunity and remaining time available to have a few conversations and learn what to look for as far as optimizations. I'm happy to say that while I did not manage to get near the lowest scores seens during the challenge I was able to further reduce the overall cost of my circuit.

Despite the challenge now being closed all material is available at Qiskit's community github repo. Follow along as I go over the four challenges using some of the material covering how I ended up solving them.

# Challenge 1 - Basic Quantum Circuits

Not so much a challenge as an introduction to using Qiskit, how quantum gates operate, and visualizing qubit states being run through these gates. The first exercise focused on interacting with a single qubit while the second exercise touched on multi-qubit interactions. As long as you followed the provided information, performed each portion step by step, and kept track of what each gate's functionality was this section was rather quick.

Each gate's functionality used throughout the exercise can be condensed down to:

Then if we take a look at an example of the type of questions were asked throughout the exercise:

**
I.i) Let us start by performing a bit flip. The goal is to reach the state |1⟩ starting from state |0⟩.
**

Most of the code was already provided for this question. The challenge was to conceptually understand what was being asked and apply the correct gate. In this case, if we applied a Hadamard gate to the qubit we would end up performing a bit flip.

Sure enough, visualizing the state with the provided code we see we were successful.

# Challenge 2 - Error Correction

The second challenge offered an opportunity to do a couple different things. Firstly, the entire premise that a physical implementation of QC would have inherent erorrs introduced requires us to interact with the physical hardware. Secondly, the challenge taught us how to introduce error correction to our specific data run and answer questions related to the data set.

The theory behind the error correction was explained well using the following:

*We start by creating a set of circuits that prepare and measure each of the \(2^{n}\) basis states, where 𝑛 is the number of qubits. For example, 𝑛=2 qubits would prepare the states |00⟩, |01⟩, |10⟩, and |11⟩ individually and see the resulting outcomes. The outcome statistics are then captured by a matrix 𝑀, where the element \(𝑀_{𝑖𝑗}\) gives the probability to get output state |𝑖⟩ when state |𝑗⟩ was prepared.
*

Some code snippets were provided to help select the least busy backend and run a calibration test for our baseline.

Then we leverage this backend to run our callibration matrix generation, while keeping an eye on the job status.

With the job run complete, we can then visualize the latent errors seen in our backend. We will use this result to apply our upcoming corrections.

As mentioned in the Challenge 2 notebook *"The goal of this exercise is to create a calibration matrix 𝑀 that you can apply to noisy results (provided by us) to infer the noise-free results"*. With our callibration matrix created we can proceed through the questions. First we visualize the provided "noisy" data, apply our error correction, then finally answer the questions by choosing which graphs resembled our corrected data sets.

# Challenge 3 - BB84

Now that we covered QC basics with challenges 1 and 2, time to move on to some practical application! The challenge 3 notebook starts by going over the theory of the BB84 Quantum Key Distribution algorithm as we will be tasked with implementing parts of it as our challenge.

### Theory

We can break down the BB84 algorithm into three parts for this challenge.

1). Alice chooses * k* bits that represent the actual information she wants to encode,

*bases that will represent how she encodes her information bit.*

**b**For \(b_i=0\) (i.e., if the \(i^{th}\) bit is zero), Alice encodes the \(i^{th}\) qubit in the standard {|0⟩,|1⟩} basis, while for \(b_i=1\), she encodes it in the {|+⟩,|−⟩} basis, where: $$ \begin{align*} \left|+\right> := \frac{1}{\sqrt{2}}(\left|0\right> + \left|1\right>) \end{align*} $$ and $$ \begin{align*} \left|-\right> := \frac{1}{\sqrt{2}}(\left|0\right> - \left|1\right>) \end{align*} $$ then sends these qubits to Bob.

2). Bob choses his own set of * b* and measures the qubits sent by Alice with these bases, storing his own set of

*.*

**k**3). Alice and Bob compare bases. For any \(i^{th}\) base that does not match, they will discard the corresponding * k*.

Once these three steps are completed, both Alice and Bob will have a shared set of information that will serve as their secret key.

For this challenge parts 1 & 2 in the above graph has been provided to us. We will first need to implement our own function to measure the "prepared qubits" that Alice sends us. Secondly, given that Alice has provided her bases, iterate through our measurements and discard those that do not match. Lastly, with our secret key with Alice defined we need to use that key to decrypt a secret message she sent us.

### Solution

For the first portion we are tasked with creating *bob_measure_qubit* in the below code portion.

To solve this portion we will need to iterate through Bob's bases, and when the base is 0, we are measuring the qubit in the Z axis so leave it as is, and if the base is 1, we are measuring the qubit in the X axis, and need to apply a Hadamard gate to the circuit before measuring.

*Side note*: I lost several hours at this point and exemplified my ingrained classical vs. quantum thinking. I had the BB84 theory right and was applying the gate properly, however when I ran the code my bits would return all 0s. It took me entirely too longer to realize I was forgetting to measure the circuit as part of the iteration. Harsh lesson.

Next, we need create a function to compare bases and keep only the measured bits where they match.

With our key established we are provided Alice's encrypted message. While iterating through the message and our key I decided to take an "easier" approach and keep the logic using strings rather than covert them to digits. I wrote my own simple variant of XOR while iterating through our strings.

The final part of this challenge was to understand the decrypted message. The challenge notebook let us know this was morse code encrypted and provided a conversion chart to get the string readable. I'll be perfectly honest and after the several hours lost forgetting to measure the circuit I did not want to strain my brain further trying to write up a snippet of code to do the conversion - so since it was a small amount of text I did it by hand in 5 minutes.

Which brings us to our answer for challenge 3 - **reddit.com/r/may4quantum**

# Challenge 4 - Circuit Decomposition

Here is where the piano was dropped on our collective heads. Reading through the brief a first time I reached the coding section and stopped a moment to scratch my head - I wasn't entirely sure what I was reading let alone what they were asking us to accomplish. This is where the community came to the rescue and I was able to boil down the challenge and a few related details that were not provided initially in the notebook. The following is a conversation seen in the slack channel during the challenge.

do we take the U gate and remake it only using single qubit rotations and cnots?

then whats the V and what does it mean to have a cost smaller than 1600?

The gate you make is called might be an approximation of U. Since it's not identical, we give it a different name: V.

U is the reference Unitary and V is the approximation, cost is calculated on the number of u3 and cx gates

CX gates cost 10, and U3s cost 1

so U gives these numbers, and we need to figure out a V which gives (very closely) the same numbers?

yes, that's the goal

Thank you random stranger for asking the question before me! Now at least there is a clear understanding of _what_ we are trying to accomplish. With that, let us start looking at the details.

### Initial Investigation

We are given a unitary U:

The shape of U is shown as (16,16) and if we print out U directly it gives us a 16x16 matrix with various values. I didn't think of this originally, however it would have been smart to plot U and notice the symmetry.

There are two important Qiskit functionalities that we will need to use for my approaches - isometry and transpile.

Firstly QuantumCircuit.iso attaches an arbitrary isometry from m to n qubits to a circuit. In particular, this allows to attach arbitrary unitaries on n qubits (m=n) or to prepare any state on n qubits (m=0). In our particular situation we can set our unitary U to our quantum circuit qc.

Secondly transpile allows us to transpile, or break down, one or more circuits into a desired target. Since our ultimate goal with this challenge is to create a circuit using only u3 and cx gates we can use transpile to reduce our target circuit to those specific gates.

With the information we have we can now code up a small sample and see where we are at.

At least our theory works and we have something to work with. It looks like even with the highest optimization level set on transpile we still have too high of a complexity cost. We will need to see what we can do to make that more efficient.

### Inefficient Solution

While I covered the solution for challenge 3 above, some might be wondering why a subreddit? If we follow the trail of crumbs we are pointed to the solution of challenge 1 as a hint for this final challenge - which came out as "Hadamard".

There was a lot of reading and attempts at understanding the theory at this point but my (potentially fallible) logic was that by multiplying a 16x16 Hadamard matrix H to the unitary U, we would have a circuit section denoted HU. The inverse of a Hadamard gate is closely related to itself, in this case \(HH^{T} = HH = I\), or the Identity matrix, so if we were to add another H circuit this should create a circuit of \(HHU = IU\). In theory, we should have a reduced circuit that approximates U.

Our circuit ends up looking like the following.

Excellent, our theory ended up working! This isn't the most efficient solution as the lowest cost recorded was 45, however I'm proud that I was able to get an initial solution.

### Optmization

While our HHU attempt worked because we added two separate circuits together, we should be able to reduce the complexity cost by doing the work prior to invoking isometry - at least that is my assumption in theory. I used scipy.linalg.hadamard in my initial solution, however I quickly found out it did not work well with this second approach. I needed to switch from generating a 16x16 Hadamard matrix (which by documentation should also be using Sylverster's construction) to using numpy.kron to compute my own 16x16 Kronecker product. If we apply this approach we start off with.

This ended up working as far as creating a more optimized circuit, however when we go to check our solution we see something off.

Hum... so our cost was drastically reduced, but the solution did not produce the right approximation of U.

Once again the community came to the rescue at this point. I did not fully understand it originally, and to be perfectly honest I am still spending time reading through the theory, however I came to learn that since we modified our unitary to be in Hadamard space, we would need to apply H gates on our qubits before and after the circuit to represent our original unitary. This brings our solution to the following.

Then if we draw this circuit out and evaluate it.

Success! Much happier with this result and although I would love to spend more time trying to get further optimization, I believe this is a good stopping point. There are quite a few concepts and theory I need to bring myself up to speed with before I can truly understand how to start approaching further improvements.

# Conclusion & Next Steps

This Quantum Challenge was a great continuation of my Quantum journey. As I mentioned in my previous QC Helloworld post I was already planning on diving into BB84 implementation. Challenge 3 offered me that chance and I definitely want to continue with implementing the algorithm more completely.

Within a day of the challenge closing IBM issued the digital badge for completing the four exercises. This is a nice addition to the overall challenge in addition to the knowledge gained.

Additionally, not that I needed further reinforcement, but this challenge highlighted just how much I don't know in the space. While I have a basic Linear Algebra background and a Comp Sci mindset, while reading some of community's the most efficient approaches to challenge 4 there were quite a few concepts I did not have enough understanding to apply myself. These will be topics of further reading in the near future.

Hopefully you all learned something while reading along with my writeup. As always thanks folks, until next time!