Quantum Program Constraints¶
When synthesizing a quantum program, you can pass in constraints to the generation; for example, requiring that no more than 53 qubits are used. The Classiq engine allows the following constraints, each of which can pass to the synthesis engine:
- Depth: the maximum depth of the quantum program
- Width: the maximum number of qubits in the quantum program
- Gate count: the maximum number of times a gate appears
Pass constraints as follows:
from classiq import Model, synthesize, set_constraints
from classiq.model import Constraints, TranspilerBasisGates
constraints = Constraints(
max_width=20,
max_depth=100,
max_gate_count={
TranspilerBasisGates.CX: 10,
TranspilerBasisGates.T: 20,
},
)
model = Model()
serialized_model = model.get_model()
serialized_model = set_constraints(serialized_model, constraints)
synthesize(serialized_model)
Alternatively, you can pass the constraints in the constraints
argument of the
synthesize
method, or change it via a property setter:
from classiq import Model, synthesize
from classiq.model import Constraints, TranspilerBasisGates
constraints = Constraints(
max_width=20,
max_depth=100,
max_gate_count={
TranspilerBasisGates.CX: 10,
TranspilerBasisGates.T: 20,
},
)
model = Model()
model.constraints = constraints
serialized_model = model.get_model()
synthesize(serialized_model)
{
"constraints": {
"max_width": 20,
"max_depth": 100,
"max_gate_count": {
"CX": 10,
"T": 20
}
},
"body": []
}
Optimization Parameter¶
When synthesizing a quantum program, to optimize the quantum program according to a
parameter, set the optimization_parameter
field. The possible
parameters are the same parameters that can be constrained.
The following example shows how to remove the width constraint in the quantum program, setting it instead as the optimization parameter.
from classiq import Model, set_constraints, synthesize
from classiq.model import Constraints, TranspilerBasisGates, OptimizationParameter
constraints = Constraints(
max_depth=100,
max_gate_count={
TranspilerBasisGates.CX: 10,
TranspilerBasisGates.T: 20,
},
optimization_parameter=OptimizationParameter.WIDTH,
)
model = Model()
serialized_model = model.get_model()
serialized_model = set_constraints(serialized_model, constraints)
synthesize(serialized_model)
{
"constraints": {
"max_depth": 20,
"max_gate_count": {
"CX": 10,
"T": 20
},
"optimization_parameter": "width"
},
"body": []
}