sf.engine.LocalEngine

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

Bases: strawberryfields.engine.BaseEngine

Local quantum program executor engine.

The Strawberry Fields engine is used to execute Program instances on the chosen local backend, and makes the results available via Result.

Example:

The following example creates a Strawberry Fields quantum Program and runs it using an engine.

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

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

We initialize the engine with the name of the local backend, and can pass optional backend options.

>>> eng = sf.Engine("fock", backend_options={"cutoff_dim": 5})

The run() method is used to execute quantum programs on the attached backend, and returns a Result object containing the results of the execution.

>>> results = eng.run(prog)
Parameters
  • backend (str, BaseBackend) – short name of the backend, or a pre-constructed backend instance

  • backend_options (None, Dict[str, Any]) – keyword arguments to be passed to the backend

print_applied([print_fn])

Print all the Programs run since the backend was initialized.

reset([backend_options])

Re-initialize the quantum computation.

run(program, *[, args, compile_options])

Execute quantum programs by sending them to the backend.

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

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.

reset(backend_options=None)[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

run(program, *, args=None, compile_options=None, **kwargs)[source]

Execute quantum programs by sending them to the backend.

Parameters
  • program (Program, Sequence[Program]) – quantum programs to run

  • args (dict[str, Any]) – values for the free parameters in the program(s) (if any)

  • compile_options (None, Dict[str, Any]) – keyword arguments for Program.compile()

Keyword Arguments
  • shots (int) – number of times the program measurement evaluation is repeated

  • modes (None, Sequence[int]) – Modes to be returned in the Result.state BaseState object. None returns all the modes (default). An empty sequence means no state object is returned.

Returns

results of the computation

Return type

Result