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 asis 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 ControlledNot 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 passess 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`
HC0X
IC1X
`
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 nonalphanumeric 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 C0X
I C1 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 put the value of qubit #0 in to superposition.
fox.set$( 1, Q.Gate.HADAMARD, [ 0 ])
// Then at moment #2
// we’ll use a ControlledNot gate
// to invert the value of qubit #1
// if the value of qubit #0 is 1.
fox.set$( 2, Q.Gate.CONTROLLED_NOT, [ 0, 1 ])
// Finally, at moment #3
// we’ll use a Pauli X gate
// to flip the value of qubit #0,
// and do the same for qubit #1.
fox.set$( 3, Q.Gate.PAULI_X, [ 0 ])
fox.set$( 3, Q.Gate.PAULI_X, [ 1 ])
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 C0 X
I C1 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.
HC0X IC1X
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 handholding in order to quickly make sense of things. With this in mind, Q offers fullon 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.
t0 t1 t2 t3 ┌───┐┌────┐┌───┐ q0 0⟩─┤ H ├┤ C0 ├┤ X │ └───┘└┬───┘└───┘ ┌┴───┐┌───┐ q1 0⟩───○──┤ C1 ├┤ 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 December 2019 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$()
00⟩ 50% chance ████████████████
01⟩ 0% chance
10⟩ 0% chance
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 multiqubit gates
Some physical architectures allow for interactioins between qubits that don’t resolve in to the simplest diagrams. Take this circuit, for example, which contains two Cnot gates at moment 2:
var cat = Q`
HC00
IC10
IC01
IC11
XI`
We can see that they overlap. The first Cnot [0] operates on qubit 0 and qubit 2, while the second Cnot [1] operates on qubit 1 and qubit 3.
We can see this slightly more clearly as a diagram
with the cat.toDiagram()
command:
t0 t1 t2 ┌───┐┌─────┐ q0 0⟩─┤ H ├┤ C00 │ └───┘└┬────┘ ┌─────┐ q1 0⟩───○──┤ C10 │ └─┬───┘ ┌┴────┐ q2 0⟩───○──┤ C01 │ └─────┘ ┌─┴───┐ q3 0⟩───○──┤ C11 │ └─────┘ ┌───┐ q4 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.)
t0 t1 t2 t3 q0 0⟩───○────○────○ q1 0⟩───○────○────○ q2 0⟩───○────○────○ q3 0⟩───○────○────○ q4 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.
t0 t1 t2 t3 t4 q0 0⟩───○────○─────○─────○ ┌───┐┌─────┐┌───┐ q1 0⟩───○──┤ H ├┤ C00 ├┤ M │ └───┘└┬────┘└───┘ ┌─────┐┌───┐ q2 0⟩───○────○──┤ C10 ├┤ M │ └─┬───┘└───┘ ┌┴────┐┌───┐ q3 0⟩───○────○──┤ C01 ├┤ M │ └─────┘└───┘ ┌─┴───┐┌───┐ q4 0⟩───○────○──┤ C11 ├┤ M │ └─────┘└───┘ ┌───┐ ┌───┐ q5 0⟩───○──┤ X ├───○───┤ M │ └───┘ └───┘
More to come! More examples! Revised examples! Check back in December 2019.
Constructor
Circuit
Function([ bandwidth: Number [, timewidth: Number ]]) => Q.Circuit
Description TK.

 index
Number
Description TK. 
 bandwidth
Number
Description TK. 
 timewidth
Number
Description TK. 
 needsEvaluation
Boolean
Description TK. 
 matrix
null or Q.Matrix
Description TK.
Static properties

 index
Number
Description TK. 
 help
Function
Description TK. 
 constants
Object
Description TK. 
 createConstant
Function
Description TK. 
 createConstants
Function
Description TK. 
 controlled
Function
Description TK. 
 expandMatrix
Function
Description TK. 
 evaluate
Function
Description TK. 
 fromText
Function
Description TK.
Prototype properties
Nondestructive methods

 clone
Function
Description TK. 
 toTable
Function
Description TK. 
 toText
Function
Description TK. 
 toDiagram
Function
Description TK. 
 toDom
Function
Description TK. 
 determineRanges
Function
Description TK. 
 copy
Function
Description TK.
Destructive methods

 ensureMomentsAreReady$
Function
Description TK. 
 fillEmptyOperations$
Function
Description TK. 
 removeHangingOperations$
Function
Description TK. 
 clearThisInput$
Function
Description TK. 
 set$
Function
Description TK. 
 cut$
Function
Description TK. 
 spliceCut$
Function
Description TK. 
 paste$
Function
Description TK. 
 splicePaste$
Function
Description TK. 
 pasteInsert$
Function
Description TK. 
 expand$
Function
Description TK. 
 trim$
Function
Description TK. 
 evaluate$
Function
Description TK. 
 report$
Function
Description TK. 
 try$
Function
Description TK.