Running a Quantum Algorithm with Strangeworks new Quantum Platform

There is now a really easy way to get into Quantum Computing and Quantum Programming. Strangeworks have made a real effort with their new platform in making it easy and simply to use, intuitive with all the functionality that you need. If you want an outline of the new service currently in beta with Strangeworks, look at a previous article on the Strangeworks Community Platform.

The Strangeworks ecosystem, allowing developers to run on a variety of Quantum Hardware and Frameworks.

Here we will walk through an example with Strangeworks provided from one of the numerous libraries that exist that can help users understand quantum algorithms and developments. The libraries provide some off the peg examples written in one of the quantum programming languages that can be loaded in seconds and run immediately. This means for learners there is not the hassle of trying to download the code, set up access to a QC and then after some time faffing around run it. The process literally takes just seconds as everything can be done from within the platform.

Strangeworks Project Library

When you first login to the platform you’ll see an option to look at the Project library. Let us select the most basic example “Play with one Qubit“, which will give us one qubit that we can explore using the QASM language. After selecting that library we can add it and then work on it after cloning the project. The code below shows how one can set up a qubit and classic register in order to manipulate it with many commands commented out with the //. For example if we look at the command x qubit[0]; this will take the state 0 and flip it to 1 and vice versa from 1 to 0.

Selecting a project from the project Library.
OPENQASM 2.0;  // Open Quantum Assembly Language to write basic quantum circiutis // QASM documentation and examples
include "";  // standard header with basic library

qreg qubit[1]; 	// Create a register of just one qubit called qubit
creg classicalbit[1]; // Create a register of just one bit called classicalbit

// z qubit[0];   		// pi rotation about the Z-axis
// x qubit[0];			// pi rotation about the X-axis
 y qubit[0];			// pi rotation about the Y-axis
// h qubit[0];          // The combination of two rotations, pi about the Z-axis followed by pi/2 about the Y-axis

// s qubit[0]; 			// S-gate, square root of Z gate
// sdg qubit[0]; 		   // conjugate of S-gate
// t qubit[0]; 			// Phase-gate, conjugate of square root of S-gate
// tdg qubit[0]; 		// conjugate of Phase-gate

// rx(0) 				// rotation around X-axis
// ry(0) 				// rotation around Y-axis
// rz(0)				// rotation around Z axis		

ry(-pi/7) 	qubit[0];   

barrier qubit[0];
measure qubit[0] -> classicalbit[0];

There are plenty of other commands such as rx(0) which performs a rotation around the x axis. The code that is un commented is y qubit[0]; and ry(-pi/7) qubit[0]; which performs a rotation around the y-axis. The last-line measures the qubit after we have performed a rotation on the qubit and puts the output in classical register.

measure qubit[0] -> classicalbit[0];

Lets run the code as it is – we don’t need to change anything. Go to the top right button which says “run code” and hit the button. Choose the number of iterations – lets go for 10. Wait a while and we get the results.

The results show that everything (all 10 of our samples result in an output of 1). Lets now make a simple modification with a Hadamard gate. We can do this by replacing both the y operations with h qubit[0]; which performs a Hadamard on our only qubit.

What does the Hadamard (H) do? This is a 2×2 matrix that performs the following operation. It takes as input a zero or one and puts that state into an equal superposition of two states. It takes a 0 and makes this into an equal super position of 0 and 1with the same constant – meaning that we should see equal numbers of 0 and 1 state.

H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}

This means we end up with a state as follows

\frac{1}{\sqrt{2}} (| 0 \rangle + | 1 \rangle)

from our original state of

| 0 \rangle

If we now look at the output from take of our samples we can see what get. Below shows the output results from 10 samples/iterations, which highlight we get 6 in the 0 state and 4 in the 1 state.

Results from the operation of a Hadamard on a single qubit from just 10 iterations using QASM.

The reason that we do not have 5 in each is due to the statistical and probabilistic nature. Let’s now run 100 samples and see if we get closer to a 50/50 % split. Illustrated below we can see we get 51 to 49. Pretty close. Thus we have seen one of the widely used operators in Quantum Computing and how it can turn a zero into a superposition using Strangeworks Community Platform.

Results from the operation of a Hadamard on a single qubit from 100 iterations using QASM, showing that there is an almost equal probability of getting a state in 0 or 1.