Cheatsheet¶
Contents
This page gives a summary of essential QCEngine commands. For more detail on how to combine these commands into full quantum programs see the quickstart section.
Working with qubits¶
A number of methods exist for helping setup and create qubits for use in QCEngine programs. In many commands qubits from either the whole QPU or a QInt object (introduced below) can be specified using either hexadecimal, binary, or decimal notation. For example, 0x4
, 0b001
and 4
would all specify the third of three qubits (having a weight of \(2^2=4\)). Multiple qubits can be selected using JavaScript’s bitwise OR operation between hexadecimal or binary values (for example, 0x2|0x4
, 0b010|0b001
and 3
all specify the second two of three qubits (having weights \(2^1=2\) and \(2^2=4\)).
qc.reset(num_qubits)
Reset the QPU and initialize it with
num_qubits
qubits.
Example:qc.reset(4)
qc.write(val, qubit)
Write the integer value
val
in binary to the qubit at locationqubit
. If noqubit
argument is passed, then write the value using all qubits in the QPU if called as a method onqc
or in the QInt if called as a method on aqint
object.
Example:qc.write(2)
qint.new(num_qubits, qint_label)
Create a QInt with the first
num_qubits
free in the QPU and label it with the stringqint_label
in circuit diagrams. QInt’s provide a way to group qubits together and act on them in these groups.
Example:var myqint = qint.new(2, 'myqint')
qc.discard()
Remvoves a specified qubit from the QPU. Can be used as a method on the qc object or a QInt object. If called without an argument then discards all qubits associated with the parent object.
Example:qc.discard(0x2);
qint.bits()
qint.peekProbability(value)
Returns the probability of observing the specified value if the parent QInt were to have all of its qubits measured. Note that although
qint.peekProbability()
can be useful for debugging QPU programs, a real QPU could not implement this function directly as in general it is not possible to determine the amplitudes (and therefore probabilities) of a QPU register’s possible configurations. Take care not to rely on it for any critical component of your QPU programs.
Example:myqint.peekProbability(2)
qint.numBits()
Returns an integer specifying the number of qubits contained within a QInt object.
Example:myqint.numBits()
qint.reverseBits(qubits)
When called with no argument, reverses the ordering of all qubits in a QInt (so that first becomes last, second becomes second from last, etc…). The argument qubits can be used to specify a subset of qubits to be reversed.
Example:myqint.reverseBits(0x1|0x2|0x4)
Single-qubit operations¶
Most single-qubit operations can either be performed as methods of the qc
object or of a qint
object. In either case, a specific qubit for the operation to act on can be specified using hexadecimal, or if no specific qubit is specified, the single-qubit operation is applied separately to all qubits in the object. When used as a qc
method, qubit specifications are relative to all qubits from the whole QPU. For qint
methods, qubit specifications are relative only to qubits in the qint
.
qc.not(qubit)
Applies the
NOT
operation to a qubit specified byqubit
. This flips the \(|0\rangle\) and|1\rangle
values. Can also be applied to a qint asmyqint.not(qubit)
. Example:qc.not(0x2)
qc.had(qubit)
(aliasqc.hadamard(qubit)
)Applies the Hadamard operation (HAD) to
qubit
, creating an equal superposition when acted on \(|0\rangle\), and an equal superposition with a relative phase of \(180^{\circ}\) when acted on \(|1\rangle\). Can also be acted on a qint asmyqint.had(qubit)
. When called without a specific qubit address, acts HAD operations on all qubits in the parent object.
Example:qc.had(0x2)
qc.phase(angle, qubit)
Applies the single-qubit
PHASE
operation to a qubit, which applies a rotation to the relative phase of a qubit. Takes two argumentsangle
(first argument) andqubit
(second argument). Theangle
argument is the rotation angle to apply (in circle notation, the angle through which the \(|1\rangle\) circle is rotated). This is equivalent to aROTZ
operation. Thequbit
argument references one or more qubits to apply the operation to. Can also be acted on a qint asmyqint.phase(angle qubit)
.
Example:qc.phase(45, 0x4)
qc.read(qubit)
Applies the
READ
operation to a qubit specified withqubit
, producing a conventional0
or1
outcome with probabilities determined by the qubits state amplitudes. Followingqc.read(qubit)
, any superposition within the qubit is destroyed. The0
/1
result ofqc.read(qubit)
can be assigned to a JavaScript variable for future use. Can also be acted on a qint asmyqint.read(qubit)
. Example:var result = qc.read(0x8)
qc.rootnot(qubit)
Applies the square root of the
NOT
operation to a qubit, such that applying the operation twice is equivalent toqc.not(qubit)
. Can also be acted on a qint asmyqint.rootnot(qubit)
.
Example:qc.rootnot(qubit)
qc.roty(angle, qubit)
Applies a rotation around the \(y\) axis of the Bloch sphere. Takes two arguments
angle
(first argument) andqubit
(second argument). Theangle
argument is the rotation angle to apply, andqubit
is the qubit to apply the rotation to. Can also be applied to a qint asmyqint.roty(angle, qubit)
. Example:qc.roty(45, 0x4)
Multi-qubit operations¶
Many single-qubit operations can be used as multi-qubit operations simply by passing an additional argument specifying an additional condition qubit, whose value determines whether or not the operation is applied to the previously specified target qubit. In many cases, when called as methods on a qint
object, single qubit operations can also be conditioned by passing a qubit argument. The qint
that the operation is called on will then be the target qubit for the operation, whilst the other qubit(s) passed as the argument to this method will be the condition qubit(s). For example, qc.phase(angle, qubit)
is a single-qubit operation, but qc.phase(angle, target_qubit, condition_qubit)
and myqint.phase(angle, condition_qubit)
are both multi-qubit operations performing a conditional phase.
qc.cnot(target_qubit, condition_qubit)
Applies the
CNOT
operation between two qubits. Takes two argumentstarget_qubit
andcondition_qubit
. The second argument (condition_qubit
) specifies the qubit whose value will determine whether or not aNOT
operation is applied to thetarget_qubit
specified in the first argument. Example:qc.cnot(0x4, 0x2)
qc.exchange(qubits)
Exchanges a number of qubits specified by the
qubits
argument. The qubits to be exchanged can be specified in binary, hexadecimal or decimal. For example, the middle two of four qubits can be exchanged using eitherqc.exchange(0b0110)
,qc.exchange(0x2|0x4)
orqc.exchange(6)
. Can also be applied to a qint asmyqint.exchange(qubits)
. Example:qc.exchange(0x2|0x4)
qc.phase(angle, control_qubit, target_qubit)
Applies a
PHASE
operation with angleangle
on the qubit specified by thetarget_qubit
argument dependent on the value of the qubit specified by thecontrol_qubit
argument. Note that this is the same method as the single qubitqc.phase(angle, qubit)
, only called with an additionaltarget_qubit
argument. Can also be applied to a qint asmyqint.phase(angle, control_qubit, target_qubit)
. Example:qc.phase(45, 0x2, 0x4)
qc.swap(qubits)
Swaps the qubits specified by the
qubits
argument. Can also be applied to a qint asmyqint.swap(qubits)
. Example:qc.swap(0x2|0x4)
qc.swap(target_qubits, control_qubit)
This alternative signature for the
qc.swap()
method allows the qubits specified by thetarget_qubits
first parameter to be swapped conditional on the value of thecontrol_qubit
specified in the second parameter. Can also be applied to a qint asmyqint.swap(target_qubits, control_qubit)
. Example:qc.swap(0x2|0x4, 0x8)
Primitives¶
This section contains a reference for commands that perform either full-blown primitives (such as the Quantum Fourier Transform), or smaller components of primitives (such as basic arithmetic functions).
qc.QFT(qubits)
Applies the Quantum Fourier Transform (QFT) circuit to all qubits specified by the
qubits
parameter. Can also be applied to a qint asmyqint.QFT(qubits)
. Example:qc.QFT(0b1110)
qc.invQFT(qubits)
Applies the inverse Quantum Fourier Transform circuit to all qubits specified by the
qubits
parameter. Can also be applied to a qint asmyqint.invQFT(qubits)
. Example:qc.invQFT(0b1110)
qc.Grover(qubits)
Applies a Grover iteration to the qubits specified in the
qubits
parameter. In Programming Quantum Computers this is referred to as theMIRROR
operation (see page 108). Can also be applied to a qint asmyqint.Grover(qubits)
. Example:qc.Grover(0x2|0x4|0x8)
qc.phase_est(q_in, q_out, cont_u)
Applies the phase estimation primitive. Takes two qint objects for its input arguments
q_in
andq_out
. The first argumentq_in
should reference a qint object containing the state for which the corresponding eigenphase is desired (this can either be an eigenstate or a superposition of eigenstates). The second argumentq_out
should provide a qint object (with qubits initialized in the \(|0\rangle\) state) that will be used to return a binary representation of the eigenphase. The number of qubits inq_out
determines the precision with which the eigenphase can be returned. Theqc.phase_est()
method also takes a third argumentcont_u
, which should be a reference to a JavaScript function that returns a controlled implementation of the operation whose eigenphases are to be determined. Can also be applied to a qint asmyqint.phase_est(q_in, q_out, cont_u)
. Example:qc.phase_est(my_input_qint, my_output_qint, cont_u_fn)
.. See code example 8.1qc.amplitude_encode(vector, myqint)
Applies the amplitude encoding algorithm for representing a JavaScript array of values (specified in the first parameter,
vector
) in the amplitudes of the qubits contained in a qint object (specified in the second parameter,myqint
). Note that vectors passed to this method should be normalized in order to be faithfully represented in state amplitudes. Example:qc.amplitude_encode([2,3,4,1], myqint)
.. See example 9.3
Arithmetic primitives¶
qint.add(value)
Adds an integer specified in the
value
parameter to the value represented in binary within a qint. Instead of an integer, thevalue
parameter can be passed a reference to another qint object which may encode a superposition of integer values. In this wayqc.add()
can perform additions between QPU registers in superposition. Example:myqint.add(5)
Example:myqint.add(myotherqint)
qint.subtract(value)
Subtracts an integer specified in the
value
parameter to the value represented in binary within a qint. Instead of an integer, thevalue
parameter can be passed a reference to another qint object which may encode a superposition of integer values. In this wayqc.add()
can perform subtractions between QPU registers in superposition. Example:myqint.subtract(5)
Example:myqint.subtract(myotherqint)
qc.addSquared(value)
Squares and then adds an integer specified in the
value
parameter to the value represented in binary within a qint. Instead of an integer, thevalue
parameter can be passed a reference to another qint object which may encode a superposition of integer values. In this wayqc.add()
can perform the addSquared operation between QPU registers in superposition. Example:myqint.addSquared(5)
Example:myqint.addSquared(myotherqint)
Formatting commands¶
qc.print(qint)
This command can be used to print values to the QCEngine UI output window. One common usage would be to print the output of a
qc.read()
operation as shown in the below example. Note consecutive calls toqc.print()
will not automatically occur on new lines in the output window. You can force new lines yourself usingqc.print("\n")
. Example:qc.print(qc.read(0x2))
qc.label(label)
Can be used to produce ‘code labels’ which group operations together in the circuit diagram shown in the QCEngine UI circuit window under a label specified by a string passed to the
label
parameter. A call toqc.label(label)
specifies the point in the code where the code label with namelabel
should begin. A second empty call to the method,qc.label("")
declares where the labelled section of the circuit should end. Example:qc.label("My label name")
qc.nop()
Used to insert space in the QCEngine UI circuit window. Useful for spacing out operations in the circuit diagram to increase readability. Example:
qc.nop()
qc_options.color_by_phase=bool
Setting this attribute of the
qc_options
object totrue
will color the filled areas in the circle notation shown in the QCEngine UI circle window according to their relative rotations. This increases visibility in differences in phases in the circle notation for QPUs containing large numbers of qubits. Note that the same effect can be achieved using the buttons in the circle window header. Example:qc_options.color_by_phase=true
qc_options.book_render=bool
Setting this attribute of the
qc_options
object totrue
will use bold needles with small circles at their tips to represent the relative rotations of circles in the QCEngine UI circle window. This increases visibility in differences in phases in the circle notation for QPUs containing large numbers of qubits. Note that the same effect can be achieved using the buttons in the circle window header. Example:qc_options.book_render=true
qc.panel_chart.widgets
qc.clearOutput()
qc.disableAnimation()
qc.disableRecording()