Circuit drawer

A Strawberry Fields module that provides an object-oriented interface for building quantum circuit representations of continuous-variable circuits using the \(\LaTeX\) Qcircuit package.

The following features of Qcircuit are currently used:

  • Loading Q-circuit: \input{Qcircuit}
  • Making Circuits: \Qcircuit
  • Spacing: @C=#1 and @R=#1
  • Wires: \qw[#1]
  • Gates: \gate {#1}, \targ, and \qswap
  • Control: \ctrl{#1}

The drawing of the following Xanadu supported operations are currently supported:

Gate type Supported gates
Single mode gates Dgate, Xgate, Zgate, Sgate, Rgate, Pgate, Vgate, Kgate, Fouriergate
Two mode gates BSgate, S2gate, CXgate, CZgate, CKgate

Note

Measurement operations MeasureHomodyne, MeasureHeterodyne, and MeasureFock are not currently supported.

Example

Circuit drawer methods

_gate_from_operator(op) Infers the number of modes and callable Circuit class method that correspond with a Strawberry Fields operator object.
parse_op(op) Transforms a Strawberry Fields operator object to a latex qcircuit gate.
_single_mode_gate(wire, circuit_op) Adds a single-mode operator gate to the circuit.
_multi_mode_gate(circuit_op, wires) Adds a multi-mode operator to the circuit.
_controlled_mode_gate(source_wire, …) Adds a controlled operator gate to the circuit.
_on_empty_column() Checks if the right-most wires for each subsystem in the circuit are all empty
_add_column() Adds a unit of quantum wire to each subsystem in the circuit.
_is_empty(op) Checks for a NOP, a quantum wire location without an operator.
_set_column_spacing(spacing) Sets visual spacing between operators in quantum circuit.
_set_row_spacing(spacing) Sets visual spacing of wires in quantum circuit.
_pad_with_spaces(string) Pads string with spaces.
dump_to_document() Writes current circuit to document.
compile_document([tex_dir]) Compiles latex documents.
_init_document() Adds the required latex headers to the document.
_end_document() Appends latex EOD code to the document.
_begin_circuit() Prepares document for latex circuit content.
_end_circuit() Ends the latex circuit content.
_end_wire() Ends a wire within the latex circuit.
_apply_spacing() Applies wire and operator visual spacing.
_write_operation_to_document(operation) Appends operation latex code to circuit in latex document.

Code details

exception strawberryfields.circuitdrawer.NotDrawableException[source]

Exception raised when a circuit is not drawable.

This class corresponds to the exception raised by parse_op() when a circuit is deemed impossible to effectively render using qcircuit.

exception strawberryfields.circuitdrawer.ModeMismatchException[source]

Exception raised when parsing a Gate object.

This class corresponds to the exception raised by parse_op() when an operator is interpreted as an n-mode gate but is applied to a number of modes != n.

exception strawberryfields.circuitdrawer.UnsupportedGateException[source]

Exception raised when attempting to add an unsupported operator.

This class corresponds to the exception raised by parse_op() when it is attempted to add an unsupported operator to the circuit.

class strawberryfields.circuitdrawer.Circuit(wires)[source]

Represents a quantum circuit that can be compiled to tex format.

Parameters:wires (int) – the number of quantum wires or subsystems to use in the circuit diagram.
_gate_from_operator(op)[source]

Infers the number of modes and callable Circuit class method that correspond with a Strawberry Fields operator object.

Parameters:op (strawberryfields.ops.Gate) – the Strawberry Fields operator object.
Returns:callable method that adds the given operator to the latex circuit. mode (int): the number of modes affected by the operator gate.
Return type:method (function)
parse_op(op)[source]

Transforms a Strawberry Fields operator object to a latex qcircuit gate.

Parameters:

op (strawberryfields.ops.Gate) – the Strawberry Fields operator object.

Raises:
_x(wire)[source]

Adds a position displacement operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_z(wire)[source]

Adds a momentum displacement operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_s(wire)[source]

Adds a squeezing operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_d(wire)[source]

Adds a displacement operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_r(wire)[source]

Adds a rotation operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_p(wire)[source]

Adds a quadratic phase shift operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_v(wire)[source]

Adds a cubic phase shift operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_k(wire)[source]

Adds a Kerr operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_fourier(wire)[source]

Adds a Fourier transform operator to the circuit.

Parameters:wire (int) – the subsystem wire to apply the operator to.
_cx(source_wire, target_wire)[source]

Adds a controlled position displacement operator to the circuit.

Parameters:
  • source_wire (int) – the controlling subsystem wire.
  • target_wire (int) – the controlled subsystem wire.
_cz(source_wire, target_wire)[source]

Adds a controlled phase operator to the circuit.

Parameters:
  • source_wire (int) – the controlling subsystem wire.
  • target_wire (int) – the controlled subsystem wire.
_ck(source_wire, target_wire)[source]

Adds a controlled Kerr operator to the circuit.

Parameters:
  • source_wire (int) – the controlling subsystem wire.
  • target_wire (int) – the controlled subsystem wire.
_bs(first_wire, second_wire)[source]

Adds a beams plitter operator to the circuit.

Parameters:
  • first_wire (int) – the first subsystem wire to apply the operator to.
  • second_wire (int) – the second subsystem wire to apply the operator to.
_s2(first_wire, second_wire)[source]

Adds an two mode squeezing operator to the circuit.

Parameters:
  • first_wire (int) – the first subsystem wire to apply the operator to.
  • second_wire (int) – the second subsystem wire to apply the operator to.
_single_mode_gate(wire, circuit_op)[source]

Adds a single-mode operator gate to the circuit.

Parameters:
  • circuit_op (str) – the latex code for the operator.
  • wires (list[int]) – a list of the indeces of subsystem wires to apply the multi-mode gate to.
_multi_mode_gate(circuit_op, wires)[source]

Adds a multi-mode operator to the circuit.

Parameters:
  • circuit_op (str) – the latex code for the operator.
  • wires (list[int]) – a list of the indeces of subsystem wires to apply the gate to.
Raises:

ModeMismatchException – if the operator is applied to non-adjacent wires.

_controlled_mode_gate(source_wire, target_wire, circuit_op)[source]

Adds a controlled operator gate to the circuit.

Parameters:
  • wire (source) – the index of the controlling subsystem.
  • target_wire (int) – the index of the controlled subsystem.
  • circuit_op (str) – the latex code for the operator.
_on_empty_column()[source]

Checks if the right-most wires for each subsystem in the circuit are all empty

Returns:whether the right-most wires for each subsystem in the circuit are all empty
Return type:bool
_add_column()[source]

Adds a unit of quantum wire to each subsystem in the circuit.

static _is_empty(op)[source]

Checks for a NOP, a quantum wire location without an operator.

Parameters:op (str) – latex code for either an operator, or empty quantum wire.
Returns:whether the argument is an empty quantum wire.
Return type:bool
_set_column_spacing(spacing)[source]

Sets visual spacing between operators in quantum circuit.

Parameters:spacing (int) – spacing between operators.
_set_row_spacing(spacing)[source]

Sets visual spacing of wires in quantum circuit.

Parameters:spacing (int) – spacing between wires.
static _pad_with_spaces(string)[source]

Pads string with spaces.

Parameters:string (str) – string to pad.
Returns:string with space added to either side.
Return type:str
dump_to_document()[source]

Writes current circuit to document.

Returns:latex document string.
Return type:str
compile_document(tex_dir='./circuit_tex')[source]

Compiles latex documents.

Parameters:tex_dir (str) – relative directory for latex document output.
Returns:the file path of the resulting latex document.
Return type:str
_init_document()[source]

Adds the required latex headers to the document.

_end_document()[source]

Appends latex EOD code to the document.

_begin_circuit()[source]

Prepares document for latex circuit content.

_end_circuit()[source]

Ends the latex circuit content.

_end_wire()[source]

Ends a wire within the latex circuit.

_apply_spacing()[source]

Applies wire and operator visual spacing.

_write_operation_to_document(operation)[source]

Appends operation latex code to circuit in latex document.

Parameters:operation (str) – the latex code for the quantum operation to be applied.