Qubits — A Shaman Perspective

A reverse engineering experiment for data engineers. Or the daring activity of understanding Quantum Computing taking the first step from an Application Programming Interface

Lorenzo M.
Published in
8 min readMar 17, 2018
A Screenshot from the game “Everything” https://en.wikipedia.org/wiki/Everything_(video_game)

“We are certainly stimulated by our experience to the creation of the concept of number — the connection of the decimal system with our ten fingers is enough to prove this. If one could imagine intelligent beings living on the sun, where everything is gaseous, they would presumably have no concept of number, any more than of “things.” They might have mathematics, but the most elementary branch would be topology. Some solar Einstein might invent arithmetic, and imagine a world to which it would be applicable, but the subject would be considered too difficult for schoolboys.” (Bertrand Russell, The Philosophy of Bertrand Russell, edited by Paul Arthur Schilpp, Evanston and Chicago: Northwestern University, 1944, p. 697.)

Philosophical Machine — A Quantum Tale

For the ones who didn’t notice yet, just a minor news: world has entered the Quantum Computing Era with the going live of IBM’s QX service running on IBM BlueMix [1].

Let’s do some Googling about…

As a software developer, I hardly walk myself through Linear Algebra and I cannot really stand Calculus completely. I can, though, usually work with Logic (thanks Sir Boole and Sir Russell) some basic cornerstones out, to create proper working algorithms; especially if well-made third-parties libraries are available to support my ignorance.

As an algorithmic exercise I write down here what I understood from taking a look at the Quantum Information Software Kit [2] and the Q# Language documentation [3], two major tools recently opensourced and published respectively by the qiskit developing initiative and Microsoft, to perform computations over the next generation of computers. Let’s see how much we can understand of a Q-programs starting from digital logic.

What is the main gate to a library? Its Application Programming Interface obviously. From my humble point of view interfaces stand to Software as Philosophy and Poetry stand to Knowledge. Interfaces contains magic, the same of words that contain signs…

Collecting Ingredients: What I think I know about Numbers

The first basic assumption is about moving from:

  • the realm of binary-based mathematics where everything is a logarithm in base-2 of something, and an integer number is usually a sequence of 8 or 16 bits read from right to left or from left to right.


  • the realm of Complex numbers, another mathematical abstraction that assumes the existence of Imaginary numbers defining quantities considered impossible to compute using Real numbers. Quantum computers are moving over Complex numbers that have an Imaginary and a Real component.

As a software developer I have to create my own device to access this quite inaccessible realm of knowledge. Let’s start with some hypothesis based on duck-typing and Functional Programming. To pass in the Realm of Imaginary Numbers we may need some thoughtful spells.

Spell no.1: Numbers can be thought as “entities” or more specifically quantities, but ontologically they are collections of properties defined over the concepts of “units” “zero” “nothing” “void” “operations” and “identity”.

Paradoxical example: “2” is a number and a quantity. Ontologically is anything but a name for something that is the addition (operation) of the unit on itself.

Spell no.2: Let’s assume that, having a so called “upper-ontology” (a dictionary that defines precisely operations on numbers) in which we have defined all operations, the units and identity for our Realm; we can pretty define any Real number for sure (by only defining the properties of this “abstract somethings” that is a number, see duck-typing: if it quacks like a duck and it walks like a duck, it is a duck!). This a common exercise, like let’s define some list of numbers starting from their GCD.

Get Best Software Deals Directly In Your Inbox

Example: check the definition for Function Composition [4]. Computer functions can be pretty similar to mathematical operations:

# composition of a multiplication and an addition
# w times z plus j equals y
y = add(multiply(w,z), j)
# the paradoxical example above explained as a computer function
unit = 1 # this is 1, the unit
add(unit, unit) # this is 2 with no "two" word!
add(add(unit, unit), unit) # and magic this is 3 with no "three" word!
# two numbers defined as multiples of their GCD
n1, n2 = 30, 15
a = multiply(gcd(n1, n2), 2)
b = gcd(n1, n2)

About Complex numbers

To move we are going to need to create an entire scroll… Stick with me.

Scroll no.1: Taking a look to Wikipedia, a Complex number [5] is defined as

c = a + bi

where “a” is the Real part and “bi” is the Imaginary part.


Scroll no.2: Note that Complex numbers can be exotic but the definition implies the very same kind of operations used by the familiar Real ones

Scroll no.3: Quantum computers can perform operations on Complex and Real numbers thanks to a new architectural design based on engineered states of matter that make possible to process information leveraging a unit called qubit.

Entering the Realm of Possibilities

About a qubit [6][7]:

a qubit is a unit of information that can have a state of 0 or 1or a state of superposition of both (0 and 1)
a Quantum computer can perform computations by leveraging the
possibility of its unit to be 0 and 1 at the same time (as above,
A superposition is a state in which the two possible states are
added (superposed). Can "Superposing" be defined as other
operations are, like adding or multiplying? Can we
add it to our "upper-ontology"

In terms of the setup we are using for this experiment, it seems we have here a new “super-operation” to add to our upper-ontology, this “operation” is the computation performed by a Quantum Circuit on a Registry made of Quantum Qubits. Let’s see in the next section what it means practically. This may be seen as a new class/level of operations/functions, e.g the properties or operations made possible thanks to Quantum states.

A New Realm: A Moebius stripe-Machine

The Imaginary number “i” is not in the realm of Real numbers (like the number that squared is -1), operations in the field of Complex numbers work really well in many models to explain physical phenomena. A lot of Mathematics that explains our perception of reality is based on computations of this kind.

In the context of my very very limited knowledge of the Quantum Physics, I cannot really grasp completely these concepts, but, look, thanks to my expertise in working with, and programming, (Web-)APIs (Application Programming Interface) and familiarity with Semantic Web concepts and Python, I can read this subject using a much more accessible language (compared to the Math involved). Accessing through Programming concepts is much easier because it leverages Logics expressed with a more “human-natural” syntax compared to Mathematics and Formal Logic. I need absolutely to try to surf these Giants’ Shoulders! [8] using my thin API-board.

The starting point for writing code is the QuantumProgram object. The QuantumProgram is a collection of circuits, …, quantum register objects, and classical register objects. The QuantumProgram methods can send these circuits to quantum hardware or simulator backends and collect the results for further analysis. [2]

… a register composed of three classical bits can store only one number at a given moment of time. Enter qubits and quantum registers: … [10]

A qubit is typically a microscopic system, such as an atom, a nuclear spin, or a polarised photon. A collection of n qubits is called a quantum register of size n. [10]

Code taken from the documentation at [2]
qiskit import QuantumProgram
qp = QuantumProgram()
# create a quantum register
qr = qp.create_quantum_register('qr', 2)
# create a binary register
cr = qp.create_classical_register('cr', 2)
# define your circuit and its registers
qc = qp.create_circuit('Bell', [qr], [cr])

The most common quantum gate is the Hadamard gate, a single qubit gate H performing the unitary transformation known as the Hadamard transform: [10]

# circuit starts from first position in the register
qc.h(qr[0]) # apply H operation on the first position of the reg

In order to entangle two (or more qubits) we have to extend our repertoire of quantum gates to two-qubit gates. The most popular two-qubit gate is the controlled-NOT (C-NOT), also known as the XOR or the measurement gate. It flips the second (target) qubit if the first (control) qubit is ∣ 1⟩ and does nothing if the control qubit is ∣ 0⟩. The gate is represented by the unitary matrix: [10]

# apply CNOT operation between first and second on the quantum register
qc.cx(qr[0], qr[1])
# measure the output
qc.measure(qr[0], cr[0])
qc.measure(qr[1], cr[1])

result = qp.execute('Bell')
# Get the histogram data of circuit 'Bell' print(result.get_counts('Bell'))

The get_counts method outputs a dictionary of state-counts pairs:

{'00': 531, '11': 493} 

The superposition of all the possible states of this circuit have frequencies (for the 00 state and 11 state) as defined in the state-counts pairs above. We can just notice how a simple two-qubits registry can generate such a complexity of superposition of states. The result of, as we called it above, a “superposition operation” is an histogram.

A more compelling example can be found in this very funny Jupyter Notebook. Printing a super-positioned emoticon.

Close enough for a Infinite Improbability Drive? [9] Probably not. But having access to a Quantum backend does the feeling at least D: After some thousands years, knowledge is still a (faithful) toss of dice (alea iacta est for quantum computing). Science is in general a good bet I suppose.


[1] IBM QX

[2] Quantum Information Software Kit

[3] The Q# Programming Language

[4] Wikipedia: Function Composition

[5] Wikipedia: Complex number

[6] Wikipedia: Qubit

[7] Wikipedia: Quantum Superposition

[8] Wikipedia: Standing on the Shoulders of Giants

[9] Wikipedia: Infinite Improbability Drive

[10] Quantwiki

Some code:

* A Quantum Computer

* https://github.com/rnowotniak/qclib

* https://pythonspot.com/an-introduction-to-building-quantum-computing-applications-with-python/

Thanks to:

* Quanta Magazine

* Nautilus

Special Thanks to:

* U. Eco

* J.L. Borges

* Moebius



Lorenzo M.

Software and Web APIs #Python #openscience #opensource