# sf.TDMProgram¶

class TDMProgram(N, name=None)[source]

Bases: strawberryfields.program.Program

Represents a photonic quantum circuit in the time domain encoding.

The TDMProgam class provides a context manager for easily defining a single time-bin of the time domain algorithm. As with the standard Program, Strawberry Fields operations are appended to the time domain program using the Python-embedded Blackbird syntax.

Once created, time domain programs can be executed on Strawberry Fields’ suite of built-in simulators in an efficient manner, or submitted to be executed on compatible hardware.

Parameters
• N (int or Sequence[int]) – If an integer, the number of concurrent (or ‘alive’) modes in each time bin. Alternatively, a sequence of integers may be provided, corresponding to the number of concurrent modes in the possibly multiple bands in the circuit.

• name (str) – the program name (optional)

Example

Below, we create a time domain program with 2 concurrent modes:

>>> import strawberryfields as sf
>>> from strawberryfields import ops
>>> prog = sf.TDMProgram(N=2)


Once created, we can construct the program using the prog.context() context manager.

>>> with prog.context([1, 2], [3, 4], copies=3) as (p, q):
...     ops.Sgate(0.7, 0) | q[1]
...     ops.BSgate(p[0]) | (q[0], q[1])
...     ops.MeasureHomodyne(p[1]) | q[0]


If we print out this program, we see that the time domain program has automated the process of repeating the single time-bin sequence constructed above:

>>> prog.print()
Sgate(0.32, 0) | (q[1])
BSgate(1, 0) | (q[0], q[1])
MeasureHomodyne(3) | (q[0])
Sgate(0.32, 0) | (q[0])
BSgate(2, 0) | (q[1], q[0])
MeasureHomodyne(4) | (q[1])
Sgate(0.32, 0) | (q[1])
BSgate(1, 0) | (q[0], q[1])
MeasureHomodyne(3) | (q[0])
Sgate(0.32, 0) | (q[0])
BSgate(2, 0) | (q[1], q[0])
MeasureHomodyne(4) | (q[1])
Sgate(0.32, 0) | (q[1])
BSgate(1, 0) | (q[0], q[1])
MeasureHomodyne(3) | (q[0])
Sgate(0.32, 0) | (q[0])
BSgate(2, 0) | (q[1], q[0])
MeasureHomodyne(4) | (q[1])


Note the ‘shifting’ of the measured registers — this optimization allows for time domain algorithms to be simulated in memory much more efficiently:

>>> eng = sf.Engine("gaussian")
>>> results = eng.run(prog)


The engine automatically takes this mode shifting into account; returned samples will always be transformed to match the modes specified during construction:

>>> print(results.all_samples)
{0: [array([1.26208025]), array([1.53910032]), array([-1.29648336]),
array([0.75743215]), array([-0.17850101]), array([-1.44751996])]}


Note that, unlike the standard Program, the TDM context manager has both required and essential arguments:

• Positional arguments are used to pass sequences of gate arguments to apply per time-bin. Within the context, these are accessible via the p context variable; p[i] corresponds to the i-th positional arguments.

All positional arguments corresponding to sequences of gate arguments must be the same length. This length determines how many time-bins are present in the TDM program.

If the p variable is not used, the gate argument is assumed to be constant across all time-bins.

• copies=1 (int): the number of times to repeat the time-domain program. For example, if a sequence of 10 gate arguments is provided, and copies==15, then there will be a total of $$10\times 15$$ time bins in the time domain program. The sequence corresponding to the gate arguments is repeated copies number of times.

• shift="default" (str or int): Defines how the qumode register is shifted at the end of each time bin. If set to "default", the qumode register is shifted such that each measured qumode reappears as a fresh mode at the beginning of the subsequent time bin. This is equivalent to a measured qumode being removed from the representation (by measurement) and a new one being added (by a light source). If set to an integer value, the register will shift by a step size of this integer at the end of each time bin.

 compile_info The device specification and the compiler that was used during compilation. num_subsystems Return the current number of valid quantum modes. register Return a tuple of all the currently valid quantum modes. target The target specification the program has been compiled against.
compile_info

The device specification and the compiler that was used during compilation.

If the program has not been compiled, this will return None.

Returns

device specification and the short name of the Compiler that was used if compiled, otherwise None

Return type

tuple or None

num_subsystems

Return the current number of valid quantum modes.

Returns

number of currently valid register subsystems

Return type

int

register

Return a tuple of all the currently valid quantum modes.

Returns

valid subsystem references

Return type

tuple[RegRef]

target

The target specification the program has been compiled against.

If the program has not been compiled, this will return None.

Returns

the short name of the target Compiler template if compiled, otherwise None

Return type

str or None

 append(op, reg) Append a command to the program. apply_op(cmd, q, t) Apply a particular operation on register q at timestep t bind_params(binding) Binds the free parameters of the program to the given values. can_follow(prev) Check whether this program can follow the given program. compile(*[, device, compiler]) Compile the program given a Strawberry Fields photonic compiler, or hardware device specification. Construct program with the register shift context(*args[, copies, shift]) Syntactic sugar for defining a Program using the with statement. draw_circuit([tex_dir, write_to_file]) Draw the circuit using the Qcircuit $$\LaTeX$$ package. Finalize the program. Simplify and optimize the program. params(*args) Create and access free circuit parameters. print([print_fn]) Print the program contents using Blackbird syntax.
append(op, reg)

Append a command to the program.

Parameters
• op (Operation) – quantum operation

• reg (list[int, RegRef]) – register subsystem(s) to apply it to

Returns

subsystem list as RegRefs

Return type

list[RegRef]

apply_op(cmd, q, t)[source]

Apply a particular operation on register q at timestep t

bind_params(binding)

Binds the free parameters of the program to the given values.

Parameters

binding (dict[Union[str, FreeParameter], Any]) – mapping from parameter names (or the parameters themselves) to parameter values

Raises

ParameterError – tried to bind an unknown parameter

can_follow(prev)

Check whether this program can follow the given program.

This requires that the final RegRef state of the first program matches the initial RegRef state of the second program, i.e., they have the same number number of RegRefs, all with identical indices and activity states.

Parameters

prev (Program) – preceding program fragment

Returns

True if the Program can follow prev

Return type

bool

compile(*, device=None, compiler=None, **kwargs)

Compile the program given a Strawberry Fields photonic compiler, or hardware device specification.

The compilation process can involve up to three stages:

1. Validation: Validates properties of the program, including number of modes and allowed operations, making sure all the Operations used are accepted by the compiler.

2. Decomposition: Once the program has been validated, decomposition are performed, transforming certain gates into sequences of simpler gates.

3. General compilation: Finally, the compiler might specify bespoke compilation logic for transforming the quantum circuit into an equivalent circuit which can be executed by the target device.

Example:

The gbs compile target will compile a circuit consisting of Gaussian operations and Fock measurements into canonical Gaussian boson sampling form.

>>> prog2 = prog.compile(compiler="gbs")


For a hardware device a DeviceSpec object, and optionally a specified compile strategy, must be supplied. If no compile strategy is supplied the default compiler from the device specification is used.

>>> eng = sf.RemoteEngine("X8")
>>> device = eng.device_spec
>>> prog2 = prog.compile(device=device, compiler="Xcov")

Parameters
• device (DeviceSpec) – device specification object to use for program compilation

• compiler (str, Compiler) – Compiler name or compile strategy to use. If a device is specified, this overrides the compile strategy specified by the hardware DevicSpec.

Keyword Arguments
• optimize (bool) – If True, try to optimize the program by merging and canceling gates. The default is False.

• warn_connected (bool) – If True, the user is warned if the quantum circuit is not weakly connected. The default is True.

Returns

compiled program

Return type

Program

construct_circuit()[source]

Construct program with the register shift

context(*args, copies=1, shift='default')[source]

Syntactic sugar for defining a Program using the with statement.

The Program object itself acts as the context manager.

draw_circuit(tex_dir='./circuit_tex', write_to_file=True)

Draw the circuit using the Qcircuit $$\LaTeX$$ package.

This will generate the LaTeX code required to draw the quantum circuit diagram corresponding to the Program.

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

Gate type

Supported gates

Single mode gates

Two mode gates

Note

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

Parameters
• tex_dir (str) – relative directory for latex document output

• write_to_file (bool) – if False, no output file is created

Returns

filename of the written tex document and the written tex content

Return type

list[str]

lock()

Finalize the program.

When a Program is locked, no more Commands can be appended to it. The locking happens when the program is run, compiled, or a successor Program is constructed, in order to ensure that the RegRef state of the Program does not change anymore.

optimize()

Simplify and optimize the program.

The simplifications are based on the algebraic properties of the gates, e.g., combining two consecutive gates of the same gate family.

Returns a copy of the program, sharing RegRefs with the original.

Returns

optimized copy of the program

Return type

Program

params(*args)

Create and access free circuit parameters.

Returns the named free parameters. If a parameter does not exist yet, it is created and returned.

Parameters

*args (tuple[str]) – name(s) of the free parameters to access

Returns

requested parameter(s)

Return type

FreeParameter, list[FreeParameter]

print(print_fn=<built-in function print>)

Print the program contents using Blackbird syntax.

Example:

# create a 3 mode quantum program
prog = sf.Program(3)

with prog.context as q:
ops.Sgate(0.54) | q[0]
ops.Sgate(0.54) | q[1]
ops.Sgate(0.54) | q[2]
ops.BSgate(0.43, 0.1) | (q[0], q[2])
ops.BSgate(0.43, 0.1) | (q[1], q[2])
ops.MeasureFock() | q

>>> prog.print()
Sgate(0.54, 0) | (q[0])
Sgate(0.54, 0) | (q[1])
Sgate(0.54, 0) | (q[2])
BSgate(0.43, 0.1) | (q[0], q[2])
BSgate(0.43, 0.1) | (q[1], q[2])
MeasureFock | (q[0], q[1], q[2])

Parameters

print_fn (function) – optional custom function to use for string printing