CircuitDrawer
Extends:
CircuitDrawer is a compiler engine which generates TikZ code for drawing quantum circuits.
The circuit can be modified by editing the settings.json file which is generated upon first execution. This includes adjusting the gate width, height, shadowing, line thickness, and many more options.
After initializing the CircuitDrawer, it can also be given the mapping
from qubit IDs to wire location (via the :meth:set_qubit_locations
function):
Example:
const circuit_backend = new CircuitDrawer()
circuit_backend.setQubitLocations({0: 1, 1: 0}) // swap lines 0 and 1
const eng = new MainEngine(circuit_backend)
... // run quantum algorithm on this main engine
console.log(circuit_backend.getLatex()) // prints LaTeX code
To see the qubit IDs in the generated circuit, simply set the `draw_id`
option in the settings.json file under "gates":"AllocateQubitGate" to
true:
"gates": {
"AllocateQubitGate": {
"draw_id": true,
"height": 0.15,
"width": 0.2,
"pre_offset": 0.1,
"offset": 0.1
},
...
The settings.json file has the following structure:
{
"control": { // settings for control "circle"
"shadow": false,
"size": 0.1
},
"gate_shadow": true, // enable/disable shadows for all gates
"gates": {
"GateClassString": {
GATE_PROPERTIES
}
"GateClassString2": {
...
},
"lines": { // settings for qubit lines
"double_classical": true, // draw double-lines for classical bits
"double_lines_sep": 0.04, // gap between the two lines for double lines
"init_quantum": true, // start out with quantum bits
"style": "very thin" // line style
}
}
All gates (except for the ones requiring special treatment) support the
following properties:
"GateClassString": {
"height": GATE_HEIGHT,
"width": GATE_WIDTH
"pre_offset": OFFSET_BEFORE_PLACEMENT,
"offset": OFFSET_AFTER_PLACEMENT,
},
Constructor Summary
Public Constructor | ||
public |
constructor(accept_input: boolean, default_measure: number) |
Member Summary
Private Members | ||
private |
|
|
private |
|
|
private |
_free_lines: *[] |
|
private |
_map: {} |
|
private |
_qubit_lines: {} |
Method Summary
Public Methods | ||
public |
Return the latex document string representing the circuit. |
|
public |
isAvailable(cmd: Command): boolean Specialized implementation of isAvailable: Returns true if the CircuitDrawer is the last engine (since it can print any command). |
|
public |
Add the command cmd to the circuit diagram, taking care of potential measurements as specified in the init function. |
|
public |
Receive a list of commands from the previous engine, print the commands, and then send them on to the next engine. |
|
public |
setQubitLocations(idToLoc: Object) Sets the qubit lines to use for the qubits explicitly. |
Public Constructors
public constructor(accept_input: boolean, default_measure: number) source
Params:
Name | Type | Attribute | Description |
accept_input | boolean | If accept_input is true, the printer queries the user to input measurement results if the CircuitDrawer is the last engine. Otherwise, all measurements yield the result default_measure (0 or 1). |
|
default_measure | number | Default value to use as measurement results if accept_input is false and there is no underlying backend to register real measurement results. |
Private Members
private _accept_input: * source
private _default_measure: * source
private _free_lines: *[] source
private _map: {} source
private _qubit_lines: {} source
Public Methods
public getLatex(): string source
Return the latex document string representing the circuit.
Simply write this string into a tex-file or, alternatively, pipe the output directly to, e.g., pdflatex:
Example:
node my_circuit.js | pdflatex
where my_circuit.js calls this function and prints it to the terminal.
public isAvailable(cmd: Command): boolean source
Specialized implementation of isAvailable: Returns true if the CircuitDrawer is the last engine (since it can print any command).
Params:
Name | Type | Attribute | Description |
cmd | Command | Command for which to check availability (all Commands can be printed). |
public printCMD(cmd: Command) source
Add the command cmd to the circuit diagram, taking care of potential measurements as specified in the init function.
Queries the user for measurement input if a measurement command arrives if accept_input was set to true. Otherwise, it uses the default_measure parameter to register the measurement outcome.
Params:
Name | Type | Attribute | Description |
cmd | Command | Command to add to the circuit diagram. |
public receive(commandList: Command[]) source
Receive a list of commands from the previous engine, print the commands, and then send them on to the next engine.
Params:
Name | Type | Attribute | Description |
commandList | Command[] | List of Commands to print (and potentially send on to the next engine). |
public setQubitLocations(idToLoc: Object) source
Sets the qubit lines to use for the qubits explicitly.
To figure out the qubit IDs, simply use the setting draw_id
in the
settings file. It is located in "gates":"AllocateQubitGate".
If draw_id is true, the qubit IDs are drawn in red.
Params:
Name | Type | Attribute | Description |
idToLoc | Object | Dictionary mapping qubit ids to qubit line numbers. |
Throw:
If the mapping has already begun (this function needs be called before any gates have been received). |