avatarSaptashwa Bhattacharyya

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4858

Abstract

g"><figcaption>Fig. 1: Distribution of the data from circuit 1 as simulated with Qiskit.</figcaption></figure><p id="4458">We can also plot the state vectors for individual qubits. Let’s do that with the following block of code-</p><div id="9ab1"><pre>statevec = <span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.Aer</span><span class="hljs-selector-class">.get_backend</span>(‘statevector_simulator’)</pre></div><div id="4277"><pre>final_state = <span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.execute</span>(circuit,statevec)<span class="hljs-selector-class">.result</span>()<span class="hljs-selector-class">.get_statevector</span>()</pre></div><div id="8f38"><pre><span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.visualization</span><span class="hljs-selector-class">.plot_bloch_multivector</span>(final_state)</pre></div><p id="6c8a">As we discussed just before the measurement applying Hadamard gates to both qubits changed switched them to X basis from Z (|0⟩, |1⟩) and the CNOT gate doesn’t alter this state. This can explain the plot below —</p><figure id="5bf4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*tz6HN0guiwwN4ISjhxuCqQ.png"><figcaption>Fig. 2: We see both qubits are now in X basis.</figcaption></figure><p id="f526">Let’s change this circuit a bit to understand better.</p><h2 id="2bed">2nd Circuit:</h2><p id="dd7b">Now let’s add an X gate before one of the Hadamard gates as below —</p><figure id="79ba"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MJYoxI00ft4c7rpltiIHkw.png"><figcaption>Circuit 2: Almost same as Circuit 1 but with an X gate extra.</figcaption></figure><p id="a761">As we have learned before that X-gate changes state from |0⟩ to |1⟩. Now one H gate is acting on state |0⟩ and another is acting at state |1⟩. You can now think about the outcome compared with the previous circuit. The tensor product between state |+⟩ (resulting from H|0⟩) and |-⟩ (resulting from H|1⟩) will be (|+⟩ ⊗ |-⟩ = |00⟩ -|01⟩+|10⟩-|11⟩). Applying a CX gate to this stage will change the stage |00⟩ -|01⟩-|10⟩+|11⟩, which is equivalent to |-⟩ ⊗ |-⟩. Measuring the probability will give us the same result as before but we can spot the difference if the state vectors are plotted. On Bloch Sphere the state vectors will be as below —</p><figure id="309e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DEHUE0yI92sSzIlPEJxN3w.png"><figcaption>Fig. 3: With Circuit 2, both qubits are now in |-⟩ state.</figcaption></figure><h2 id="01f0">3rd Circuit :</h2><p id="3ac2">To understand the Bloch sphere representation and circuits with multiple qubits even in more detail, let’s make a small tweak to circuit 2. Instead of applying the X gate to 1st qubit, we apply it to 0th qubit. Let’s build the circuit</p><div id="081a"><pre><span class="hljs-attribute">qr2</span> <span class="hljs-operator">=</span> q.QuantumRegister(<span class="hljs-number">2</span>) <span class="hljs-attribute">cr2</span> <span class="hljs-operator">=</span> q.ClassicalRegister(<span class="hljs-number">2</span>)</pre></div><div id="5883"><pre><span class="hljs-attr">circuit2</span> = q.QuantumCircuit(qr2, cr2)</pre></div><div id="ae72"><pre><span class="hljs-attribute">circuit2</span>.x(qr2[<span class="hljs-number">0</span>]) # X-gate applied to <span class="hljs-number">0</span>th qubit <span class="hljs-attribute">circuit2</span>.h(qr2[<span class="hljs-number">0</span>]) <span class="hljs-attribute">circuit2</span>.h(qr2[<span class="hljs-number">1</span>]) <span class="hljs-attribute">circuit2</span>.cx(qr2[<span class="hljs-number">0</span>], qr2[<span class="hljs-number">1</span>])</pre></div><div id="5a4e"><pre>circuit<span class="hljs-number">2.</span>draw<span class="hljs-comment">()</span></pre></div><figure id="06b3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wSDpzwQPhNfbHfrFxIgicQ.png"><figcaption>Circuit 3: ‘Almost’ same as 2nd circuit, but X gate at 0th qubit.</figcaption></figure><p id="5998">As we see here, compared to the previous circuit (Circuit 2), the X gate has interchanged the position. H gate acting on state |0⟩ and |1⟩ will give same result. But the tensor product will be different |-⟩ ⊗ |+⟩ = |-+⟩ = (|00⟩ + |01⟩ -|10⟩-|11⟩). Here we apply the CX gate and it won’t affect anything. Plotting the Bloch sphere of the state vectors after executing the circuit should result in qubit 0 in |-⟩ and qubit 1 in |+⟩ state. Let’s see —</p><figure id="0704"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IxV6Cruje3FdMmrgw2vaHA.png"><figcaption>Fig. 4: As expected from circuit 3, 0th qubit in |-⟩ and 1st qubit in |+⟩ state after execution.</figcaption></figure><p id="4461">Simulating the circuit in our workstation will result something simil

Options

ar as in fig. 2 with all 4 states present in equal probability.</p><h2 id="239d">4th Circuit:</h2><p id="1a74">Let’s add some more gates to get a stronger grip on our understanding. Compared to the 1st circuit, we will add one more Hadamard gate after the CX gate as below —</p><div id="70de"><pre><span class="hljs-attribute">qr3</span> <span class="hljs-operator">=</span> q.QuantumRegister(<span class="hljs-number">2</span>) <span class="hljs-attribute">cr3</span> <span class="hljs-operator">=</span> q.ClassicalRegister(<span class="hljs-number">2</span>)</pre></div><div id="dd67"><pre><span class="hljs-attr">circuit3</span> = q.QuantumCircuit(qr3, cr3)</pre></div><div id="a19e"><pre>circuit3<span class="hljs-selector-class">.h</span>(qr3<span class="hljs-selector-attr">[0]</span>) circuit3<span class="hljs-selector-class">.h</span>(qr3<span class="hljs-selector-attr">[1]</span>)</pre></div><div id="f57f"><pre>circuit3<span class="hljs-selector-class">.cx</span>(qr3<span class="hljs-selector-attr">[0]</span>, qr3<span class="hljs-selector-attr">[1]</span>)</pre></div><div id="7b69"><pre><span class="hljs-meta prompt_">#</span><span class="language-bash"><span class="hljs-comment"># till this is first circuit. Now we add one more H gate.</span></span></pre></div><div id="d287"><pre>circuit3<span class="hljs-selector-class">.h</span>(qr3<span class="hljs-selector-attr">[0]</span>)</pre></div><div id="e0b3"><pre>circuit<span class="hljs-number">3.</span>draw<span class="hljs-comment">()</span></pre></div><figure id="f4df"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QzSVQMg8x7EFb_ZifEYw_g.png"><figcaption>Circuit 4: Added H gate compared to Circuit 1.</figcaption></figure><p id="0550">Can you guess/calculate the final state of the qubits? We have already seen the answer up to the CX gate before. CX gate was applied upon the state |++⟩ and the state of the system remained unchanged. Now we added another H gate. This H gate is added to the 0th qubit. H|++⟩ = |+0⟩ , because application of H gate to state |+⟩ will take it back to |0⟩. Once again we can see this by plotting the final state vectors.</p><figure id="3016"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MrReGBqLLOPXexkv-SicyQ.png"><figcaption>Fig. 5: 0th qubit at state |0⟩, 1st qubit at state |+⟩.</figcaption></figure><p id="be69">The tensor product |+⟩ ⊗ |0⟩ will give us |00⟩ + |10⟩ and this can also be tested by simulating the circuit in our local computer and plotting the distribution as we have done for circuit 1.</p><figure id="4c80"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*R2T21DR39qK4uIfDJJm72g.png"><figcaption>Fig. 6: |00⟩ and |10⟩ are present with almost 50% probability as expected from circuit 4.</figcaption></figure><h2 id="bf8f">5th Circuit:</h2><p id="4c81">Finally, we conclude with another variation of the previous circuit. Instead of adding the last Hadamard gate to 0th qubit we add it on to 1st qubit. So the circuit would look as below —</p><figure id="ce0f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M4I8DH3Y0qt6oRLtmEKxuQ.png"><figcaption>Circuit 5: Hadamard Gate is now added to 1st qubit.</figcaption></figure><p id="27ec">Comapred to the previous circuit, the tensor product will be as below — |0+⟩ and we can see that using the Bloch Sphere visualization</p><figure id="5148"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aHNgGBwJ36qTI32LbH8nAg.png"><figcaption>Fig. 7: Now 0th qubit at state |+⟩ and 1st qubit at state |0⟩.</figcaption></figure><p id="bf35">The final states when simulated in local computer will give rise to 50% probability for each of |00⟩ and |01⟩ state.</p><figure id="7f2f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*J7nmLE1NQDvI2fYNbsFtww.png"><figcaption>Fig. 8: |00⟩ and |01⟩ are present with 50% probability for each.</figcaption></figure><h2 id="4081">Conclusion:</h2><p id="2f4a">In summary, to strengthen our understanding of simple circuits with multiple qubits, we discussed various circuits and checked our understanding by simulating those circuits in our local computer and visualizing the state vectors in a Bloch Sphere. In the previous two posts we have gone through the building blocks of quantum circuits — matrix representations of quantum gates, entanglement, Bell state etc. The circuits discussed here are extension of the previous ones and will be important later on to build up a necessary concept called Phase Kickback, which will provide the framework to understand many core algorithms ex: Shor’s algorithm, Deutsch algorithm etc.</p><p id="ad42">Python Notebook used here is available in my <a href="https://github.com/suvoooo/Qubits-Qiskit/blob/master/Simple_Circuits.ipynb">GitHub</a>.</p><p id="fe96">Stay strong, Cheers !!</p></article></body>

Simple Quantum Circuits to Understand Multiple Qubit States

Quantum Circuits and Bloch Sphere

Reflection ! (Image Source: Author)

Previously I have described about building simple quantum circuits and thinking quantum gates as matrix operators. Also using superposition of qubits, we learnt to how to create entangled states. If you suddenly find yourself here in this post without reading the previous two posts, it may be little difficult to follow along. Before diving deep into an important concept known as ‘Phase Kickback’, in this post I will describe various quantum circuits and how to build intuition to get started with multiple qubits and quantum gates. Since Bloch Sphere is often used to visualize quantum states, we will also learn how to visualize and understand the quantum states. Let’s get in !!

1st Circuit:

In the last post I have described how to create entangled state using one Hadamard gate and one CNOT (CX) gate. First we create a superposition state by applying the Hadamard gate, then we apply the CX gate. Instead of using one Hadamard gate what happens if we apply 2 Hadamard gates? Let’s first build the circuit —

import qiskit as q
import matplotlib.pyplot as plt
qr = q.QuantumRegister(2)
cr = q.ClassicalRegister(2)
circuit = q.QuantumCircuit(qr, cr)
circuit.h(qr[0]) 
# apply hadamard gate on first qubit.
circuit.h(qr[1]) 
# apply hadamard gate on second qubit.
# apply cnot gate.
circuit.cx(qr[0], qr[1]) 
display(circuit.draw())

Applying two Hadamard gates and then CX gate with control bit at first qubit, we can get to the following circuit diagram —

Circuit 1: Two Hadamard gates and a CX gate

What we are expecting at this stage? Initial stages of the qubits are always zero unless they are initialized to a different state. So H gate acting on stage |0⟩ would give us |+⟩. Since there are 2 H gates acting on stage |0⟩ we will get tensor product of |+⟩ ⊗ |+⟩ as output. |+⟩ ⊗ |+⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩) and operating the CX gate on this stage will not change anything. So if we simulate this circuit in our workstation, we will gate all 4 stages with almost equal probabilities.

circuit.measure(qr, cr)
simulator = q.Aer.get_backend(name=’qasm_simulator’)
results = q.execute(circuit, backend=simulator, ).result()
q.visualization.plot_histogram(results.get_counts(circuit))
Fig. 1: Distribution of the data from circuit 1 as simulated with Qiskit.

We can also plot the state vectors for individual qubits. Let’s do that with the following block of code-

statevec = q.Aer.get_backend(‘statevector_simulator’)
final_state = q.execute(circuit,statevec).result().get_statevector()
q.visualization.plot_bloch_multivector(final_state)

As we discussed just before the measurement applying Hadamard gates to both qubits changed switched them to X basis from Z (|0⟩, |1⟩) and the CNOT gate doesn’t alter this state. This can explain the plot below —

Fig. 2: We see both qubits are now in X basis.

Let’s change this circuit a bit to understand better.

2nd Circuit:

Now let’s add an X gate before one of the Hadamard gates as below —

Circuit 2: Almost same as Circuit 1 but with an X gate extra.

As we have learned before that X-gate changes state from |0⟩ to |1⟩. Now one H gate is acting on state |0⟩ and another is acting at state |1⟩. You can now think about the outcome compared with the previous circuit. The tensor product between state |+⟩ (resulting from H|0⟩) and |-⟩ (resulting from H|1⟩) will be (|+⟩ ⊗ |-⟩ = |00⟩ -|01⟩+|10⟩-|11⟩). Applying a CX gate to this stage will change the stage |00⟩ -|01⟩-|10⟩+|11⟩, which is equivalent to |-⟩ ⊗ |-⟩. Measuring the probability will give us the same result as before but we can spot the difference if the state vectors are plotted. On Bloch Sphere the state vectors will be as below —

Fig. 3: With Circuit 2, both qubits are now in |-⟩ state.

3rd Circuit :

To understand the Bloch sphere representation and circuits with multiple qubits even in more detail, let’s make a small tweak to circuit 2. Instead of applying the X gate to 1st qubit, we apply it to 0th qubit. Let’s build the circuit

qr2 = q.QuantumRegister(2)
cr2 = q.ClassicalRegister(2)
circuit2 = q.QuantumCircuit(qr2, cr2)
circuit2.x(qr2[0]) # X-gate applied to 0th qubit 
circuit2.h(qr2[0])
circuit2.h(qr2[1])
circuit2.cx(qr2[0], qr2[1])
circuit2.draw()
Circuit 3: ‘Almost’ same as 2nd circuit, but X gate at 0th qubit.

As we see here, compared to the previous circuit (Circuit 2), the X gate has interchanged the position. H gate acting on state |0⟩ and |1⟩ will give same result. But the tensor product will be different |-⟩ ⊗ |+⟩ = |-+⟩ = (|00⟩ + |01⟩ -|10⟩-|11⟩). Here we apply the CX gate and it won’t affect anything. Plotting the Bloch sphere of the state vectors after executing the circuit should result in qubit 0 in |-⟩ and qubit 1 in |+⟩ state. Let’s see —

Fig. 4: As expected from circuit 3, 0th qubit in |-⟩ and 1st qubit in |+⟩ state after execution.

Simulating the circuit in our workstation will result something similar as in fig. 2 with all 4 states present in equal probability.

4th Circuit:

Let’s add some more gates to get a stronger grip on our understanding. Compared to the 1st circuit, we will add one more Hadamard gate after the CX gate as below —

qr3 = q.QuantumRegister(2)
cr3 = q.ClassicalRegister(2)
circuit3 = q.QuantumCircuit(qr3, cr3)
circuit3.h(qr3[0])
circuit3.h(qr3[1])
circuit3.cx(qr3[0], qr3[1])
## till this is first circuit. Now we add one more H gate.
circuit3.h(qr3[0])
circuit3.draw()
Circuit 4: Added H gate compared to Circuit 1.

Can you guess/calculate the final state of the qubits? We have already seen the answer up to the CX gate before. CX gate was applied upon the state |++⟩ and the state of the system remained unchanged. Now we added another H gate. This H gate is added to the 0th qubit. H|++⟩ = |+0⟩ , because application of H gate to state |+⟩ will take it back to |0⟩. Once again we can see this by plotting the final state vectors.

Fig. 5: 0th qubit at state |0⟩, 1st qubit at state |+⟩.

The tensor product |+⟩ ⊗ |0⟩ will give us |00⟩ + |10⟩ and this can also be tested by simulating the circuit in our local computer and plotting the distribution as we have done for circuit 1.

Fig. 6: |00⟩ and |10⟩ are present with almost 50% probability as expected from circuit 4.

5th Circuit:

Finally, we conclude with another variation of the previous circuit. Instead of adding the last Hadamard gate to 0th qubit we add it on to 1st qubit. So the circuit would look as below —

Circuit 5: Hadamard Gate is now added to 1st qubit.

Comapred to the previous circuit, the tensor product will be as below — |0+⟩ and we can see that using the Bloch Sphere visualization

Fig. 7: Now 0th qubit at state |+⟩ and 1st qubit at state |0⟩.

The final states when simulated in local computer will give rise to 50% probability for each of |00⟩ and |01⟩ state.

Fig. 8: |00⟩ and |01⟩ are present with 50% probability for each.

Conclusion:

In summary, to strengthen our understanding of simple circuits with multiple qubits, we discussed various circuits and checked our understanding by simulating those circuits in our local computer and visualizing the state vectors in a Bloch Sphere. In the previous two posts we have gone through the building blocks of quantum circuits — matrix representations of quantum gates, entanglement, Bell state etc. The circuits discussed here are extension of the previous ones and will be important later on to build up a necessary concept called Phase Kickback, which will provide the framework to understand many core algorithms ex: Shor’s algorithm, Deutsch algorithm etc.

Python Notebook used here is available in my GitHub.

Stay strong, Cheers !!

Quantum Computing
Quantum Mechanics
Programming
Qiskit
Quantum Circuits
Recommended from ReadMedium