In this post, we build a program to show quantum superposition. Let us start with a quantum bit in a basis state |0>. Operations are executed on the qubit and the result is evaluated.
Superposition is a quantum mechanics principle that allows quantum interference. It is a wave property that can be represented in mathematical probability for. Sum of two waves with amplitude and phase is called a superposition. Two waves can be added and subtracted based on the phase value. Quantum interference occurs when two quantum wave functions are added. Superpositions occur when quantum wave functions with complex coefficients are added. To summarize, superposition is the possibility of an electron being at different positions at the same time or different directions in a spin.
Power of Quantum bit is in its quantum mechanical properties such as superposition and quantum entanglement. Quantum bit can be in both zero state and one state simultaneously. An increase in the number of quantum bits will also increase exponentially the number of possible entangled states. The quantum state can be one of the following:
- Electron with spin
- Photon with polarization
- Impurity spins
- trapped ions state
- neutral atom state
- state of semiconducting circuits
The code sample below shows the Quantum.qs after adding the operation Set and SuperpositionStateTest.
Code Snippet : Quantum Q#Example
namespace Superposition
{
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Primitive;
operation Set (desired: Result, q1: Qubit) : Unit
{
let current = M(q1);
if (desired != current)
{
X(q1);
}
}
The set operation updates the qubit to the desired state of zero or one. The quantum bit is measured which collapses the state to either 0 or 1. If the desired is not equal to the current, X (NOT gate) flips it otherwise nothing is changed.
operation SuperpositionStateTest (count : Int, initial: Result) : (Int, Int)
{
mutable numOnes = 0;
using (qubit = Qubit())
{
for (test in 1..count)
{
Set (initial, qubit);
H(qubit);
let res = M (qubit);
if (res == One)
{
set numOnes = numOnes + 1;
}
}
Set(Zero, qubit);
}
return (count-numOnes, numOnes);
}
}
SuperpositionStateTest operation takes parameters count and initial. The operation returns two values as a tuple of type (Int, Int). This operation loops the count number of times and measures one and zero states. The measurement happens after invoking the set operation and Hamard Gate on Qubit.
The Driver file will have the following code to execute SuperpositionState Test.
Code Sample : Driver C# code
using System;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
namespace SuperpositionDriver
{
class Driver
{
static void Main(string[] args)
{
using (var qsim = new QuantumSimulator())
{
Result[] initials = new Result[] { Result.Zero, Result.One };
foreach (Result initial in initials)
{
var res = SuperpositionStateTest.Run(qsim, 1000, initial).Result;
var (numZeros, numOnes) = res;
System.Console.WriteLine(
$”Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}”);
}
}
}
}
}
The number of times zero and one values come in the run of 1000 times is printed for superposition test. Quantum bit is halfway between 0 and 1 as shown statistically below in the output.
Init:Zero 0s=506 1s=494
Init:One 0s=497 1s=503
The screenshot of the output is attached below:
