sf.compilers.Passive

class Passive[source]

Bases: strawberryfields.compilers.compiler.Compiler

Compiler to write a sequence of passive operations as a single passive operation

This compiler checks whether the circuit can be implemented as a sequence of passive operations. If so, it arranges them in a single matrix, T. It then returns an PassiveChannel operation which can act this transformation.

This compiler can be accessed by calling Program.compile() with ‘passive’ specified.

Example:

Consider the following Strawberry Fields program, compiled using the ‘passive’ compiler:

from strawberryfields.ops import BSgate, LossChannel, Rgate
import strawberryfields as sf

circuit = sf.Program(2)
with circuit.context as q:
    Rgate(np.pi) | q[0]
    BSgate(0.25 * np.pi, 0) | (q[0], q[1])
    LossChannel(0.9) | q[1]

compiled_circuit = circuit.compile(compiler="passive")

We can now print the compiled circuit, consisting a single PassiveChannel:

>>> compiled_circuit.print()
PassiveChannel([[-0.7071+8.6596e-17j -0.7071+0.0000e+00j]
 [-0.6708+8.2152e-17j  0.6708+0.0000e+00j]]) | (q[0], q[1])

circuit

A rigid circuit template that defines this circuit specification.

decompositions

graph

The allowed circuit topologies or connectivity of the class, modelled as a directed acyclic graph.

interactive

primitives

short_name

circuit

A rigid circuit template that defines this circuit specification.

If arbitrary topologies are allowed in the circuit class, this function will simply return None.

If a backend device expects a specific template for the received Blackbird script, this method will return the serialized Blackbird circuit in string form.

Returns

Blackbird circuit or template representing the circuit

Return type

Union[str, None]

decompositions = {}
graph

The allowed circuit topologies or connectivity of the class, modelled as a directed acyclic graph.

This property is optional; if arbitrary topologies are allowed in the circuit class, this will simply return None.

Returns

a directed acyclic graph

Return type

networkx.DiGraph

interactive = True
primitives = {'All', 'BSgate', 'Interferometer', 'LossChannel', 'MZgate', 'PassiveChannel', 'Rgate', '_Delete', '_New_modes', 'sMZgate'}
short_name = 'passive'

add_loss(program, device)

Adds realistic loss to circuit.

compile(seq, registers)

Try to arrange a passive circuit into a single multimode passive operation

decompose(seq)

Recursively decompose all gates in a given sequence, as allowed by the circuit specification.

init_circuit(layout)

Sets the circuit in the compiler class.

reset_circuit()

Resets the circuit and graph class attributes.

update_params(program, device)

Updates and checks parameters in the program circuit.

add_loss(program, device)

Adds realistic loss to circuit.

Child classes which are hardware compilers should override this method with device specific loss added to the circuit.

compile(seq, registers)[source]

Try to arrange a passive circuit into a single multimode passive operation

This method checks whether the circuit can be implemented as a sequence of passive gates. If the answer is yes it arranges them into a single operation.

Parameters
  • seq (Sequence[Command]) – passive quantum circuit to modify

  • registers (Sequence[RegRefs]) – quantum registers

Returns

compiled circuit

Return type

List[Command]

Raises

CircuitError – the circuit does not correspond to a passive unitary

decompose(seq)

Recursively decompose all gates in a given sequence, as allowed by the circuit specification.

This method follows the directives defined in the primitives and decompositions class attributes to determine whether a command should be decomposed.

The order of precedence to determine whether decomposition should be applied is as follows.

  1. First, we check if the operation is in decompositions. If not, decomposition is skipped, and the operation is applied as a primitive (if supported by the Compiler).

  2. Next, we check if (a) the operation supports decomposition, and (b) if the user has explicitly requested no decomposition.

    • If both (a) and (b) are true, the operation is applied as a primitive (if supported by the Compiler).

    • Otherwise, we attempt to decompose the operation by calling decompose() recursively.

Parameters

list[strawberryfields.program_utils.Command] – list of commands to be decomposed

Returns

list of compiled commands for the circuit specification

Return type

list[strawberryfields.program_utils.Command]

classmethod init_circuit(layout)

Sets the circuit in the compiler class.

Parameters

layout (str) – the circuit layout for the target device

Return type

None

classmethod reset_circuit()

Resets the circuit and graph class attributes.

Return type

None

update_params(program, device)

Updates and checks parameters in the program circuit.

Child classes can override this method with compiler specific logic. If no parameters need to be updated, and are separately checked, this method should not be overridden and be left empty.

Parameters
  • program (Program) – Program containing the circuit and gate parameters

  • device (DeviceSpec) – device specification containing the valid parameter values

Return type

None