Source code: Q-Circuit.js


The sounds of circuitry

Quantum circuits are represented by diagrams similar to musical staves. When a musician reads a line of music she begins at moment zero on the left edge and proceeds forward in time by reading to the right. In this example we can see from the ornate figure on the left that the pitch of the notes will be described by a C clef and that each measure will span three beats thanks to the 3/4 time signature notation.



Similarly, quantum circuit diagrams are a series of moments in time, beginning at moment zero on the left edge and proceeding forward in time by reading to the right. Here we have a quantum circuit that is 3 moments long and operates on just 2 quantum bits:


Let’s read it from left to right, observing what happens at each passing moment. We begin at moment zero (t0) with our initial qubit values. Both qubit #0 and qubit #1 begin with a value of 0. This is a “horizontal” qubit state. In normal quantum circuit design these initial values will always be 0. Q, however, allows us to manually change these input values to any possible qubit state so that you may simulate a snippet of a complete circuit. We’ll come back to this later, but for now let’s read on.

During our first moment of true operation (t1) we apply a Hadamard gate to qubit #0, putting it in to a superposition state. (This is represented by the “H” within a square on qubit #0’s circuit wire.) We leave qubit #1 as-is by applying an Identity gate; a null operator or “do nothing” gate. This is represented by the small circle on qubit #1’s circuit wire.

In our next moment (t2) we operate on both qubits at once by applying a Controlled-Not gate. This operation will flip the value of qubit #1 if, and only if, the value of qubit #0 collapses to 1. This action entangles the two qubits; their states are now dependent on one another. At this point it is impossible to compute the values of either qubit #0 or qubit #1 independently. From here onward the state of the circuit can only be computed as a whole.

In our circuit’s final moment (t3) we apply a Pauli X gate to each qubit, flipping the value of both.

Writing quantum circuits

To create the above circuit we can take advantage of several Q shortcuts. To beging with, the Q object itself is a function that internally passes its arguments to the Circuit.fromText() static method. This function can accept text as an argument, but also accepts Template literals, which use backticks rather than single or double quotes, and do not require parentheses to invoke a function call. Note the backticks and lack of parentheses in this example. We’ll call our example circuit “fox”:


var fox = Q`

	H-X#0-X
	I-X#1-X
`

Q is rather flexible when it comes to parsing text into circuits. New lines indicate a new qubit to operate on. Within a line, any non-alphanumeric character between alphanumerics is interpreted as a moment seperator. Therefore, the following (less sensible) input creates a circuit identical to the one above.


var fox = Q`H X#0--X
I  X#1   X`

Internally, fromText is making the following calls to create the circuit. Note the extreme difference between the very little we must type (above) to create the circuit and the large amount of construction happening under the hood (below).


//  Create a circuit 
//  that operates on 2 qubits
//  and lasts for 3 moments.

var fox = new Q.Circuit( 2, 3 )


	//  At moment #1 (the first moment we can operate),
	//  we’ll use a Hadamard gate
	//  to set the value of the qubit 
	//  on register #1 in to superposition.

	.set$( Q.Gate.HADAMARD, 1, 1 )


	//  Then at moment #2
	//  we’ll use a Controlled-Not gate
	//  to invert the value on register #2
	//  if the value of register #1 is 1.
	//  Note this is actually created from a 
	//  Pauli X gate with two inputs!

	.set$( Q.Gate.PAULI_X, 2, [ 1, 2 ])


	//  Finally, at moment #3
	//  we’ll use a Pauli X gate
	//  to flip the value of register #1,
	//  and do the same for register #2.

	.set$( Q.Gate.PAULI_X, 3, 1 )
	.set$( Q.Gate.PAULI_X, 3, 2 )

I think you’ll agree the shorter Q`…` syntax is preferable.

Inspecting quantum circuits

Let’s create that fox circuit again using our compressed Q syntax. (This illustrates yet another small variation on the whitespace and gate separator used in the input Template literal.)


var fox = Q`

	H  X#0  X
	
	I  X#1  X
`

We can fully inspect our circuit as an object in the JavaScript console, of course. But we can also get a summary overview with the following command.


fox.toText()

This returns the following string.


H-X#0-X
I-X#1-X

Does that look familiar? It’s very nearly the text we used to construct our circuit in the first place. In fact, because we can both construct and ouput circuits using text we can clone and test for equality between circuits like so.


var dog = Q( fox.toText() )

fox === dog//  false.
fox.toText() === dog.toText()//  true.

Though in practice you’re far more likely to clone a circuit by using its own method for doing so.


var dog = fox.clone()

dog === fox//  false.
fox.toText() === dog.toText()//  true.

ASCII circuit diagrams

While outputting a circuit as simple text can be useful, we humans often need a little more visual hand-holding in order to quickly make sense of things. With this in mind, Q offers full-on ASCII circuit diagrams.


fox.toDiagram()

The above command yields the following, with time labeled from t0 (the moment of input) progressing onward, and qubits labeled likewise.


         m1    m2    m3  
        ┌───┐╭─────╮┌───┐
r1  |0⟩─┤ H ├┤ X#0 ├┤ X │
        └───┘╰──┬──╯└───┘
             ╭──┴──╮┌───┐
r2  |0⟩───○──┤ X#1 ├┤ X │
             ╰─────╯└───┘

Interactive circuit diagrams

But this is a Web browser—we ought to make use of its document object model for interactivity, no?! The toDom() method returns a document fragment, complete with event handlers, that can be attached to your live document.

Note that this feature is still in its early stages and is not yet fully functional. Check back in May 2020 for updates. Better yet, contribute to Q!

document.body.appendChild( fox.toDom() )

This yields the following:

Executing quantum circuits

Once a circuit is created it must be evaluated with evaluate$(). This resolve’s the circuit’s state matrix and also notes the state names (binary digits) and their corresponding propabilities in the instance’s outcomes property. And that’s good. But what’s even better is seeing those results! A circuit’s report$() method will internally call evaluate$() if need be, then log out the possible outcomes, including bar graphs of the probabilities.


fox.report$()

1  |00⟩  ██████████░░░░░░░░░░  50% chance
2  |01⟩  ░░░░░░░░░░░░░░░░░░░░   0% chance
3  |10⟩  ░░░░░░░░░░░░░░░░░░░░   0% chance
4  |11⟩  ██████████░░░░░░░░░░  50% chance

For fun we can also use try$() to randomly pick an outcome based on the outcome probabilities. Similar to report$(), the try$() method will internally call evaluate$() if need be.


fox.try$()
|00⟩

fox.try$()
|11⟩

Overlapping multi-qubit gates

Some physical architectures allow for interactions between qubits that don’t resolve in to the simplest diagrams. Take this circuit, for example, which contains two C-not gates at moment 2:


var cat = Q`

	H  X.0#0
	I  X.1#0
	I  X.0#1
	I  X.1#1
	X  I
`

We can see that they overlap. The first C-not [0] operates on qubit 0 and qubit 2, while the second C-not [1] operates on qubit 1 and qubit 3. We can see this slightly more clearly as a diagram with the cat.toDiagram() command:


         m1     m2    
        ┌───┐╭───────╮
r1  |0⟩─┤ H ├┤ X.0#0 │
        └───┘╰──┬────╯
             ╭───────╮
r2  |0⟩───○──┤ X.1#0 │
             ╰────┬──╯
             ╭──┴────╮
r3  |0⟩───○──┤ X.0#1 │
             ╰───────╯
             ╭────┴──╮
r4  |0⟩───○──┤ X.1#1 │
             ╰───────╯
        ┌───┐         
r5  |0⟩─┤ X ├────○──  
        └───┘        

Editing quantum circuits

Copy, cut, paste

Let’s start with the cat circuit above, and copy a section of it. In this case we’re copying all of its moments and all of its qubits so it is effectively equal to cat.clone():


var gup = cat.copy({
	
	qubitFirstIndex:  0,
	qubitRange:       cat.bandwidth,
	momentFirstIndex: 0,
	momentRange:      cat.timewidth
})

Both copy and cut$ are very flexible with the parameter object. Supplying no parameters is effectively equal to selecting the entire circuit. (So the above example is a bit superfluous, but socratic.)

Cutting replaces the “cut” operations with Identity gates and returns the cut portion.


var hen = gup.cut$()

Cutting all of the operations as above will yield a blank circuit as seen below. (And hen will now contain those cut operations.)


    m0   m1   m2   m3  
                     
r0  |0⟩───○────○────○
                     
                     
r1  |0⟩───○────○────○
                     
                     
r2  |0⟩───○────○────○
                     
                     
r3  |0⟩───○────○────○
                     
                     
r4  |0⟩───○────○────○
                     

Pasting in to a circuit can cause that circuit to expand in both time and qubits. For example, both our cat and gup circuits are 4 moments long (0, 1, 2, 3) and 5 qubits in bandwidth (0, 1, 2, 3, 4). What if we paste our cat circuit in to our empty gup circuit but do so by pushing it one moment further and one qubit further?


gup.paste$( cat, 1, 1 )

Notice how gup has now expanded. The first qubit registers are still empty. The first moment of qubits also remains empty. But the entirety of cat is now pasted in there.


    m0   m1   m2     m3     m4  
                              
r0  |0⟩───○────○──────○──────○  
                              
             ┌───┐┌───────┐┌───┐
r1  |0⟩───○──┤ H ├┤ X.0#0 ├┤ M │
             └───┘└─┬─────┘└───┘
                  ┌───────┐┌───┐
r2  |0⟩───○────○──┤ X.1#0 ├┤ M │
                  └───┬───┘└───┘
                  ┌─┴─────┐┌───┐
r3  |0⟩───○────○──┤ X.0#1 ├┤ M │
                  └───────┘└───┘
                  ┌───┴───┐┌───┐
r4  |0⟩───○────○──┤ X.1#1 ├┤ M │
                  └───────┘└───┘
             ┌───┐         ┌───┐
r5  |0⟩───○──┤ X ├────○────┤ M │
             └───┘         └───┘

More to come! More examples! Revised examples! Check back in May 2020.


Constructor

Circuit Function([ bandwidth: Number [, timewidth: Number ]]) => Q.Circuit
Description TK.

Static properties

Prototype properties

Non-destructive methods

Destructive methods