sf.io.generate_code

generate_code(prog, eng=None)[source]

Converts a Strawberry Fields program into valid Strawberry Fields code.

Example:

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

with prog.context as q:
    ops.Sgate(2*np.pi/3) | q[1]
    ops.BSgate(0.6, 0.1) | (q[2], q[0])
    ops.MeasureFock() | q

results = eng.run(prog)

code_str = sf.io.utils.generate_code(prog, eng=eng)

This will create the following string:

>>> print(code_str)
import strawberryfields as sf
from strawberryfields import ops

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

with prog.context as q:
    ops.Sgate(2*np.pi/3, 0.0) | q[1]
    ops.BSgate(0.6, 0.1) | (q[2], q[0])
    ops.MeasureFock() | (q[0], q[1], q[2])

results = eng.run(prog)
Parameters
  • prog (Program) – the Strawberry Fields program

  • eng (Engine) – The Strawberryfields engine. If None, only the Program parts will be in the resulting code-string.

Returns

the Strawberry Fields code, for constructing the program, as a string

Return type

str