My few observations while I prepared for the Qiskit Certification exam (1)
This and the following blog are about my observations related to a couple of topics covered in the Qiskit Certification exam that I managed to pass last year. I have been wanting to write about this since then and I am finally doing it. This blog is not a tutorial or guide for how to prepare for the exam. Some people from the Qiskit community have already done a great job in creating resources for the exam. If you are interested in learning about Qiskit and taking the exam, I would recommend you join the Qiskit slack workspace (link as of now)and there is a channel dedicated to the exam in the workspace where there are pinned messages about useful resources created by the community members. The Qiskit documentation links provided in the study guide are definitely very useful. You could also follow the QuantumGrad youtube channel and some articles specific to the exam preparation on the QuantumGrad website. All other articles on the website can also be checked. You can sign up on this website and write your own articles.
Coming to the first observation. This is related to circuit depth and barrier operations.
My first experience with Barrier operations was during the IBM Quantum Challenge in Fall 2020. That challenge was super interesting like every IBM Quantum Challenge and its focus was solving puzzles using Grover’s algorithm and optimizing the circuits. In one of the challenge questions, my circuit was huge, the grader was detecting it wrong and I was just not able to figure out where I was wrong. After 2 days of struggling, I just asked a mentor to give me hints on where I could be going wrong. The mentor suggested I use barriers which would help me debug my circuit. I did it and I immediately realized what was wrong and finally managed to pass the grader. At that moment, I regarded Barrier operation as just an operation that can help visualize circuits and help in debugging huge circuits. In this challenge, we were only working with quantum simulators. In the subsequent IBM Quantum Challenges, where we were running our circuits on actual IBM quantum hardware, we were told to remove barriers and that made me think that perhaps, there is something more to Barrier. Afterall, if it was just some operation to help visualization, there shouldn’t have been any problem running it on the hardware. Last year, out of curiosity, I started reading about OpenQASM and found barrier statements to be present there as well. Finally, it was while preparing for the Qiskit Certification exam that I realized I really need to understand Barrier operation better.
One of the topics in the Certification exam is circuit depth. This documentation on qiskit.org (it is under the Qiskit circuit properties dropdown) where it is explained that the circuit depth can be calculated by playing Tetris, is very interesting. This post on Stack exchange really explains very well what circuit depth is and how it is calculated. We will see how Barrier affects the circuit depth. Consider the following circuit:
This circuit has depth 2 and it’s very easy to see that, after we understand circuit depth from the links above. Let’s see what happens when we add barrier operations:
It’s exactly the same circuit with a barrier added after the 1st gate on qubit 0, but that has increased the circuit depth by 1. Let us see more examples with the same circuit. Suppose we add a barrier after 1st gate, but only between qubits 0 and 1, like below.
We see that even in this case, the circuit depth has changed to 3. Suppose, we had instead put the barrier from qubits 1 and 2.
We can actually see from the circuit diagram that this Barrier is really not adding much value and that’s also clear from the circuit depth which is 2, same as the first circuit that had no barriers at all. You can see the same below:
From the above 2 circuits, it was clear to me that the barrier only adds to the depth if it is exactly separating 2 consecutive gates, which is kind of intuitive. More examples below:
From the last 2 circuits, we can see that a barrier between the Pauli Z gate on qubit 2 (q_r2) and the ccx gate with target as qubit 2, is not really adding to the circuit depth, but if we remove the barrier between the ccx (target as qubit 2. Though the ccx gate involves control qubits 0 and 1, the ultimate qubit operation is only happening on qubit 2) and the H gate on qubit 3, the depth will reduce by 1 and we will get it as 4, see below:
This means that the barriers add to circuit depth when they are between 2 consecutive gates on different qubits. So, this is important to remember if we have to calculate circuit depth by just looking at a circuit that has barriers.
The conclusion is that the Barrier operation affects the circuit depth and it is indeed doing something more than just helping us visualize huge circuits. So for the exam, I just kept my statement ‘barriers add to circuit depth when they are between 2 consecutive gates on different qubits’, in mind, but after the exam, I wanted to understand Barriers more. After reading a bit, I understood that Barrier operations are not standard operations generally discussed in Quantum Information Theory. Their role in Qiskit or any other Quantum Computing framework is basically to help in optimization of a circuit, in blocks. I also found this discussion on Github very interesting and useful. So without barriers, transpile would optimize on the entire circuit, but with barriers, it would look at blocks between every two barriers and optimize these blocks individually. Finally, just a few months ago, this video on the Qiskit youtube channel, was definitely an eye-opener. I would highly recommend watching that quick video if one wants to understand Barriers. Thank you for reading! :)