sf.circuitspecs.CircuitSpecs

class CircuitSpecs[source]

Bases: abc.ABC

Abstract base class for describing circuit classes.

This class stores information about classes of quantum circuits. For some circuit classes (e.g, ones corresponding to physical hardware chips), the specifications can be quite rigid. For other classes, e.g., circuits supported by a particular simulator backend, the specifications can be more flexible and general.

Key ingredients in a specification include: the primitive gates supported by the circuit class, the gates that can be decomposed to sequences of primitive gates, and the possible topology/connectivity restrictions.

This information is used e.g., in Program.compile() for validation and compilation.

circuit

A rigid circuit template that defines this circuit specification.

decompositions

Quantum operations that are not quantum primitives for the circuit class, but are supported via specified decompositions.

graph

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

interactive

Whether the circuits in the class can be executed interactively, that is, the registers in the circuit are not reset between engine executions.

local

Whether the circuit class can be executed locally (i.e., within a simulator).

modes

The number of modes supported by the circuit class.

parameter_ranges

Allowed parameter ranges for supported quantum operations.

primitives

The primitive set of quantum operations directly supported by the circuit class.

remote

Whether the circuit class supports remote execution.

short_name

short name of the circuit class

circuit

A rigid circuit template that defines this circuit specification.

This property is optional. If arbitrary topologies are allowed in the circuit class, do not define this property. In such a case, it will simply return None.

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

Returns

Blackbird program or template representing the circuit

Return type

Union[str, None]

decompositions

Quantum operations that are not quantum primitives for the circuit class, but are supported via specified decompositions.

This should be of the form

{'operation_name': {'option1': val, 'option2': val,...}}

For each operation specified in the dictionary, the Operation.decompose() method will be called during Program compilation, with keyword arguments given by the dictionary value.

Returns

the quantum operations that are supported by the circuit class via decomposition

Return type

dict[str, dict]

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

Whether the circuits in the class can be executed interactively, that is, the registers in the circuit are not reset between engine executions.

Returns

True if the circuit supports interactive use

Return type

bool

local

Whether the circuit class can be executed locally (i.e., within a simulator).

Returns

True if the circuit class supports local execution

Return type

bool

modes

The number of modes supported by the circuit class.

If the circuit class supports arbitrary number of modes, set this to 0.

Returns

number of supported modes

Return type

int

parameter_ranges

Allowed parameter ranges for supported quantum operations.

This property is optional.

Returns

a dictionary mapping an allowed quantum operation to a nested list of the form [[p0_min, p0_max], [p1_min, p0_max], ...]. where pi corresponds to the i th gate parameter

Return type

dict[str, list]

primitives

The primitive set of quantum operations directly supported by the circuit class.

Returns

the names of the quantum primitives the circuit class supports

Return type

set[str]

remote

Whether the circuit class supports remote execution.

Returns

True if the circuit class supports remote execution

Return type

bool

short_name = ''

short name of the circuit class

Type

str

compile(seq, registers)

Class-specific circuit compilation method.

decompose(seq)

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

compile(seq, registers)[source]

Class-specific circuit compilation method.

If additional compilation logic is required, child classes can redefine this method.

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

  • registers (Sequence[RegRefs]) – quantum registers

Returns

modified circuit

Return type

List[Command]

Raises

CircuitError – the given circuit cannot be validated to belong to this circuit class

decompose(seq)[source]

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 CircuitSpecs).

  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 CircuitSpecs).

    • 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]