avatarSaptashwa Bhattacharyya

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

3889

Abstract

"><figcaption>Fig 2: Histogram distribution of output from 2 H gates in parallel.</figcaption></figure><h2 id="741f">3 H gates in Parallel:</h2><p id="24ce">Extending the previous simple circuit, we consider 3 Hadamard gates in parallel. Following the math before for 2 H gates in parallel, for 3 H gates we will have a tensor product as — (H⊗ H⊗ H)(|0⟩|0⟩|0⟩) , this can be written more mathematically as below —</p><figure id="818b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*n-0vJvfdvRHa98DHpXrmlA.jpeg"><figcaption></figcaption></figure><p id="192e">Just as before the state vectors and the circuit diagram can be drawn using qiskit —</p><figure id="11ea"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5VP9w_k_RvbehqaARM9UqQ.jpeg"><figcaption>Fig. 3: Same as Fig.1 but now 3 H gates are in parallel and corresponding state vectors are shown in right panel.</figcaption></figure><p id="3fc0">We can simulate the circuit in our local computer and plot the histogram distribution of the output —</p><div id="8ce5"><pre><span class="hljs-attribute">qr1</span> <span class="hljs-operator">=</span> q.QuantumRegister(<span class="hljs-number">3</span>)</pre></div><div id="4862"><pre><span class="hljs-attribute">cr1</span> <span class="hljs-operator">=</span> q.ClassicalRegister(<span class="hljs-number">3</span>)</pre></div><div id="a843"><pre><span class="hljs-attr">circuit1</span> = q.QuantumCircuit(qr1, cr1)</pre></div><div id="ff45"><pre><span class="hljs-attribute">circuit1</span>.h(qr1[<span class="hljs-number">0</span>]) # apply the H gate <span class="hljs-literal">on</span> q_0</pre></div><div id="044b"><pre><span class="hljs-attribute">circuit1</span>.h(qr1[<span class="hljs-number">1</span>]) # apply H gate <span class="hljs-literal">on</span> q_1</pre></div><div id="6385"><pre><span class="hljs-attribute">circuit1</span>.h(qr1[<span class="hljs-number">2</span>]) # add H gate <span class="hljs-literal">on</span> q_2</pre></div><div id="682c"><pre>circuit<span class="hljs-number">1.</span>measure<span class="hljs-comment">(qr1, cr1)</span></pre></div><div id="df46"><pre>### simulate result on local computer</pre></div><div id="fcb9"><pre>simulator1 = <span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.Aer</span><span class="hljs-selector-class">.get_backend</span>(‘qasm_simulator’)</pre></div><div id="dc7d"><pre>results1 = <span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.execute</span>(circuit1, backend=simulator1, )<span class="hljs-selector-class">.result</span>()</pre></div><div id="79bd"><pre><span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.visualization</span><span class="hljs-selector-class">.plot_histogram</span>(results1<span class="hljs-selector-class">.get_counts</span>(circuit1))</pre></div><figure id="8d51"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1p5PxwtE_e5vbbhHbdwSNQ.png"><figcaption>Fig. 4: Output of 3H gates in parallel simulated in a local computer</figcaption></figure><p id="9d19">We have discussed before that each outcome will have a probability ~1/8=0.125 and we can see that on the histogram plot (Fig. 4).</p><p id="de48">We can simulate the same circuit in a quantum computer using <a href="https://quantum-computing.ibm.com/">IBM Quantum Experience</a>. Getting started with IBM-Q experience was discussed before in a <a href="https://readmedium.com/quantum-computing-understanding-simple-quantum-gates-using-qiskit-6bb96f52793b">separate post</a>. Let’s see the results of running the same circuit on a real quantum computer —</p><div id="5209"><pre><span class="hljs-keyword">from</span> qiskit <span class="hljs-keyword">import</span> IBMQ</pre></div><div id="063e"><pre>IBMQ.save_account(‘your <span class="hljs-built_in">token</span>’)</pre></div><div id="b24f"><pre>IBMQ

Options

<span class="hljs-selector-class">.load_account</span>()</pre></div><div id="5dfe"><pre><span class="hljs-attribute">provider</span> <span class="hljs-operator">=</span> IBMQ.get_provider(‘ibm-q’)</pre></div><div id="aba5"><pre><span class="hljs-attr">qcomp</span> = provider.get_backend(‘ibmq_valencia’) <span class="hljs-comment"># check which computer has 0 jobs on queue</span></pre></div><div id="6c3c"><pre><span class="hljs-attr">job</span> = q.execute(circuit1, backend=qcomp)</pre></div><div id="7aee"><pre><span class="hljs-attr">q_result</span> = job.result()</pre></div><div id="e811"><pre><span class="hljs-selector-tag">q</span><span class="hljs-selector-class">.visualization</span><span class="hljs-selector-class">.plot_histogram</span>(q_result<span class="hljs-selector-class">.get_counts</span>(circuit1))</pre></div><figure id="5775"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*eqabioE6T8wpnzWaukSgHQ.png"><figcaption>Fig. 5: Same as Fig. 4, but now the output is obtained using a real quantum computer than a simulator in local computer.</figcaption></figure><p id="dbad">Now we see a considerable deviation from a probability of 1/8 for each of the output. This is because, the qubits are in assumed to be prefect state in the simulator and can be manipulated with perfect precision, these qubits can be considered as <i>logical qubits</i>. But, the <a href="https://www.nature.com/articles/s41534-018-0095-x">physical systems</a> (superconducting or trapped ion quantum computers) that behave as qubits are prone to be imprecise and susceptible to small quantum errors. However, this error mitigation is a constant ongoing process and one of the active areas of research.</p><h2 id="6ab6">Math of Uniform Superposition:</h2><p id="511b">By studying the examples of 2 and 3 H gates in parallel, we can see common pattern for the final output. If we look back again to the 2H gates in parallel, the output was of the form 0.5(|00⟩ + |01⟩ + |10⟩ + |11⟩) and this can be written compactly in a form —</p><figure id="e153"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HRl50arvZzimeHBtW1GkMg.jpeg"><figcaption></figcaption></figure><p id="d1ef">Here,<i> </i>|<i>x</i>⟩ denotes one of the states |00⟩, |01⟩, |10⟩, |11⟩. If we write <i>x</i>∈ {0, 1}³<i>, </i>then <i>x</i> denotes one of the states —</p><p id="79ed">|000⟩, |001⟩, |010⟩, |011⟩, |100⟩, |101⟩, |110⟩, |111⟩. Using this notations, we can generalize a rule for <i>n</i> H gates in parallel —</p><figure id="2c3d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*z0r3aG82n4F18yLroMGs7Q.jpeg"><figcaption></figcaption></figure><h2 id="9e20">Quantum Interference:</h2><p id="8772">Finally, I will end the post with a small discussion on quantum interference. We have discussed this a bit in the <a href="https://readmedium.com/quantum-computing-bell-state-and-entanglement-with-qiskit-621489fb36bd">entanglement post</a>. If we consider a quantum state |ψ⟩ = α |0⟩ + β |1⟩ and apply H gate then we obtain —</p><figure id="73c3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VA_-n0N4fbJQ_H4nshIBWw.jpeg"><figcaption></figcaption></figure><p id="0818">We can think about the above expression as <i>positive interference</i> w.r.t to the basis state |0⟩ (since the two amplitude terms α, β are added) and <i>negative interference</i> w.r.t basis state |1⟩. This concept is used to gain information about a function <i>f(x)</i> that depends on evaluating the function at many values of <i>x</i> . We will use all these concepts to discuss some of the most important quantum algorithms, soon!</p><p id="793b">All the above codes and mathematics involved can be found in my <a href="https://github.com/suvoooo/Qubits-Qiskit/blob/master/Hadamard_Superposition.ipynb">GitHub</a>.</p><p id="a651">Stay strong and cheers !</p></article></body>

Multiple Hadamard Gates in Parallel: Uniform Superposition in Quantum Computing

When N Hadamard Gates Act on N Qubits

Image Credit: Author

One of the most important and commonly occurring multiple qubit gates is Hadamard gate. Before we have seen matrix representation of Hadamard gate and on a separate post studied several simple quantum circuits involving Hadamard gate. In this post we will explore uniform superposition, which is the basis of Grover’s Algorithm. This post will be short and involve some mathematics but if you have gone through the previous posts and have a grasp on graduate level math, this won’t be difficult at all. Let’s get started!

2 Hadamard Gates in Parallel:

We have discussed this circuit before and here let’s review it once again. We get started by importing ‘qiskit’ and then adding 2 H gates respectively to 2 qubits in parallel. Below is the code for creating this simple circuit.

!pip3 install qiskit
import qiskit as q 
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.
circuit.measure(qr, cr)
circuit.draw(scale=2)

We have learnt before how to use qiksit to display the circuit and plot the state vectors and below is a picture —

Fig.1 : Left: Circuit diagram of 2H gates in parallel. Right: Representing 2 state vectors in a Bloch Sphere following the circuit on the left. H|0⟩ creates |x⟩, for more see here.

Math of 2 H gates in Parallel:

Let’s review the math behind 2 parallel H gates in short. 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 |+⟩ = (|0⟩ + |1⟩)/sqrt(2). Since there are 2 H gates in parallel acting on stage |0⟩, we have the output as tensor product of |+⟩ ⊗ |+⟩ = 0.5 (|00⟩ + |01⟩ + |10⟩ + |11⟩). We can simulate the circuit in our local computer and plot a histogram distribution of the output as below —

Fig 2: Histogram distribution of output from 2 H gates in parallel.

3 H gates in Parallel:

Extending the previous simple circuit, we consider 3 Hadamard gates in parallel. Following the math before for 2 H gates in parallel, for 3 H gates we will have a tensor product as — (H⊗ H⊗ H)(|0⟩|0⟩|0⟩) , this can be written more mathematically as below —

Just as before the state vectors and the circuit diagram can be drawn using qiskit —

Fig. 3: Same as Fig.1 but now 3 H gates are in parallel and corresponding state vectors are shown in right panel.

We can simulate the circuit in our local computer and plot the histogram distribution of the output —

qr1 = q.QuantumRegister(3)
cr1 = q.ClassicalRegister(3)
circuit1 = q.QuantumCircuit(qr1, cr1)
circuit1.h(qr1[0]) # apply the H gate on q_0
circuit1.h(qr1[1]) # apply H gate on q_1
circuit1.h(qr1[2]) # add H gate on q_2
circuit1.measure(qr1, cr1)
### simulate result on local computer
simulator1 = q.Aer.get_backend(‘qasm_simulator’)
results1 = q.execute(circuit1, backend=simulator1, ).result()
q.visualization.plot_histogram(results1.get_counts(circuit1))
Fig. 4: Output of 3H gates in parallel simulated in a local computer

We have discussed before that each outcome will have a probability ~1/8=0.125 and we can see that on the histogram plot (Fig. 4).

We can simulate the same circuit in a quantum computer using IBM Quantum Experience. Getting started with IBM-Q experience was discussed before in a separate post. Let’s see the results of running the same circuit on a real quantum computer —

from qiskit import IBMQ
IBMQ.save_account(‘your token’)
IBMQ.load_account()
provider = IBMQ.get_provider(‘ibm-q’)
qcomp = provider.get_backend(‘ibmq_valencia’) # check which computer has 0 jobs on queue
job = q.execute(circuit1, backend=qcomp)
q_result = job.result()
q.visualization.plot_histogram(q_result.get_counts(circuit1))
Fig. 5: Same as Fig. 4, but now the output is obtained using a real quantum computer than a simulator in local computer.

Now we see a considerable deviation from a probability of 1/8 for each of the output. This is because, the qubits are in assumed to be prefect state in the simulator and can be manipulated with perfect precision, these qubits can be considered as logical qubits. But, the physical systems (superconducting or trapped ion quantum computers) that behave as qubits are prone to be imprecise and susceptible to small quantum errors. However, this error mitigation is a constant ongoing process and one of the active areas of research.

Math of Uniform Superposition:

By studying the examples of 2 and 3 H gates in parallel, we can see common pattern for the final output. If we look back again to the 2H gates in parallel, the output was of the form 0.5(|00⟩ + |01⟩ + |10⟩ + |11⟩) and this can be written compactly in a form —

Here, |x⟩ denotes one of the states |00⟩, |01⟩, |10⟩, |11⟩. If we write x∈ {0, 1}³, then x denotes one of the states —

|000⟩, |001⟩, |010⟩, |011⟩, |100⟩, |101⟩, |110⟩, |111⟩. Using this notations, we can generalize a rule for n H gates in parallel —

Quantum Interference:

Finally, I will end the post with a small discussion on quantum interference. We have discussed this a bit in the entanglement post. If we consider a quantum state |ψ⟩ = α |0⟩ + β |1⟩ and apply H gate then we obtain —

We can think about the above expression as positive interference w.r.t to the basis state |0⟩ (since the two amplitude terms α, β are added) and negative interference w.r.t basis state |1⟩. This concept is used to gain information about a function f(x) that depends on evaluating the function at many values of x . We will use all these concepts to discuss some of the most important quantum algorithms, soon!

All the above codes and mathematics involved can be found in my GitHub.

Stay strong and cheers !

Qiskit
Quantum Computing
Programming
Python
Quantum Superposition
Recommended from ReadMedium