sf.engine.BaseEngine

class BaseEngine(backend, *, backend_options=None)[source]

Bases: abc.ABC

Abstract base class for quantum program executor engines.

Parameters
  • backend (str) – backend short name

  • backend_options (Dict[str, Any]) – keyword arguments for the backend

get_tdm_options(program, **kwargs)

Retrieves the shots and modes for the TDM program and (space)unrolls it if necessary.

print_applied([print_fn])

Print all the Programs run since the backend was initialized.

reset(backend_options)

Re-initialize the quantum computation.

static get_tdm_options(program, **kwargs)[source]

Retrieves the shots and modes for the TDM program and (space)unrolls it if necessary.

print_applied(print_fn=<built-in function print>)[source]

Print all the Programs run since the backend was initialized.

This will be blank until the first call to run(). The output may differ compared to Program.print(), due to backend-specific device compilation performed by run().

Example:

# create a program
prog = sf.Program(2)

with prog.context as q:
    ops.S2gate(0.543) | (q[0], q[1])

# create an engine
eng = sf.Engine("gaussian")

Initially, the engine will have applied no operations:

>>> eng.print_applied()
None

After running the engine, we can now see the quantum operations that were applied:

>>> eng.run(prog)
>>> eng.print_applied()
Run 0:
BSgate(0.7854, 0) | (q[0], q[1])
Sgate(0.543, 0) | (q[0])
Sgate(0.543, 0).H | (q[1])
BSgate(0.7854, 0).H | (q[0], q[1])

Note that the S2gate has been decomposed into single-mode squeezers and beamsplitters, which are supported by the 'gaussian' backend.

Subsequent program runs can also be viewed:

# a second program
prog2 = sf.Program(2)
with prog2.context as q:
    ops.S2gate(0.543) | (q[0], q[1])
>>> eng.run(prog2)
>>> eng.print_applied()
Run 0:
BSgate(0.7854, 0) | (q[0], q[1])
Sgate(0.543, 0) | (q[0])
Sgate(0.543, 0).H | (q[1])
BSgate(0.7854, 0).H | (q[0], q[1])
Run 1:
Dgate(0.06, 0) | (q[0])
Parameters

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

abstract reset(backend_options)[source]

Re-initialize the quantum computation.

Resets the state of the engine and the quantum circuit represented by the backend.

  • The original number of modes is restored.

  • All modes are reset to the vacuum state.

  • All registers of previously run Programs are cleared of measured values.

  • List of previously run Progams is cleared.

Note that the reset does nothing to any Program objects in existence, beyond erasing the measured values.

Example:

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

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

# create an engine
eng = sf.Engine("gaussian")

Running the engine with the above program will modify the state of the statevector simulator:

>>> eng.run(prog)
>>> eng.backend.is_vacuum()
False

Resetting the engine will return the backend simulator to the vacuum state:

>>> eng.reset()
>>> eng.backend.is_vacuum()
True

Note

The reset() method only applies to statevector backends.

Parameters

backend_options (Dict[str, Any]) – keyword arguments for the backend, updating (overriding) old values