Serialization API¶
Program¶
The object returned by the program context manager. It can be serialized
to and from a protobuf binary for storage or transfer.
Program ¶
The Program class encapsulates a QuaProgram protobuf object along with additional metadata.
It provides properties to access the program's state, including the capabilities used and
whether it is currently being edited (is_in_scope). Additionally, it includes methods to
serialize and deserialize the QuaProgram to and from various formats.
This class is used internally during construction (within ProgramScope) and externally
as a read-only API for users. Protected methods (prefixed with _) are intended only
for internal use during the building phase.
from_file
classmethod
¶
Deserialize the program from a protobuf binary file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The file path to read the serialized program from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Program
|
The deserialized program. |
from_protobuf
classmethod
¶
Deserialize the program from a protobuf binary.
| PARAMETER | DESCRIPTION |
|---|---|
binary
|
The protobuf binary to deserialize.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Program
|
The deserialized program. |
to_file ¶
Serialize the program to a protobuf binary and write it to a file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The file path to write the serialized program to.
TYPE:
|
config
|
The QUA configuration to embed in the serialized program.
TYPE:
|
to_protobuf ¶
Serialize the program to a protobuf binary.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
The QUA configuration to embed in the serialized program.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
The serialized program as a protobuf binary. |
Script Generation and Comparison¶
Functions for turning a Program into a standalone Python script, and for comparing two programs
for structural equality.
generate_qua_script ¶
Serializes a QUA program into a runnable QUA Python script.
The returned string is standalone Python source that reconstructs the given program, useful for debugging, sharing, or archiving a program.
Note
This function must be called outside of a program() scope.
Note
In some cases the serialization may be incomplete. When that happens, the generated
script silently contains a SERIALIZATION WAS NOT COMPLETE comment block with the
raw protobuf representation of the program. This indicates a problem in the
serialization itself and should be reported to Quantum Machines.
| PARAMETER | DESCRIPTION |
|---|---|
prog
|
The QUA
TYPE:
|
config
|
A QUA configuration to embed in the generated script. When omitted, the script is generated without a configuration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The generated QUA Python script as a string. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If called inside a QUA program scope, or if the given config is invalid. |
assert_programs_are_equal ¶
Asserts that two QUA programs are structurally equivalent.
Both programs are first normalized with standardize_program_for_comparison, so that
differences that do not affect runtime behavior (source locations, variable and stream
names, result-analysis ordering) are ignored.
| PARAMETER | DESCRIPTION |
|---|---|
prog1
|
The first program to compare.
TYPE:
|
prog2
|
The second program to compare.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the two programs are not structurally equivalent. |