Skip to content

Model Inputs & Outputs

You can include the inputs and outputs in the model, just as for composite functions. These definitions have important implications for the clarity, applicability, and quality of the code.

If you do not define registers as inputs or wire them to another function, the Classiq platform assumes that they begin at state zero. Input registers must be assigned "new" qubits, but ones assumed to begin at zero may be assigned released qubits. As a result, non-input registers may be assigned only later in the quantum program, using previously used qubits. This allows the synthesis engine to reduce the qubit count of the quantum program. However, when registers reuse qubits it is not possible to then initiate them with non-zero values. Therefore, you should define whether registers play the functional roles of inputs. As a rule, you should assign model inputs only if they may receive non-zero values.

Setting inputs and outputs may help you understand the logic of complex quantum programs. You can use qubit indices—both logical in the quantum program and physical on the hardware (if specified)—for all model inputs and outputs.

To measure output qubits, use the counts_of_output method of the execution result. This helps you verify the functionality of a quantum program by executing it on a quantum simulator or hardware. See execution for more details and methods.

Below are usage examples of model inputs and outputs. Note the similarity to the examples in composite functions:

  • In the textual model, use wires as for any other function call.
  • In the SDK, use the set_outputs() and create_inputs() methods.
{
  "functions": [
    {
      "name": "main",
      "port_declarations": {
        "minuend": {
          "name": "minuend",
          "size": 3,
          "direction": "input"
        },
        "subtrahend": {
          "name": "subtrahend",
          "size": 3,
          "direction": "input"
        },
        "difference": {
          "name": "difference",
          "size": 4,
          "direction": "output"
        }
      },
      "body": [
        {
          "function": "Negation",
          "function_params": {
            "arg": { "size": 3 },
            "inplace": true
          },
          "inputs": { "arg": "subtrahend_in" },
          "outputs": { "negated": "negation_output_wire" }
        },
        {
          "function": "Adder",
          "function_params": {
            "left_arg": { "size": 3 },
            "right_arg": { "size": 4 }
          },
          "inputs": {
            "left_arg": "minuend_in",
            "right_arg": "negation_output_wire"
          },
          "outputs": { "sum": "difference_out" }
        }
      ]
    }
  ]
}
from classiq import Model, RegisterUserInput, QUInt
from classiq.builtin_functions import Adder, Negation

subtrahend = RegisterUserInput(size=3)
minuend = RegisterUserInput(size=3)
negation_params = Negation(arg=subtrahend, inplace=True)
adder_params = Adder(
    left_arg=minuend, right_arg=negation_params.outputs[negation_params.output_name]
)

model = Model()
input_dict = model.create_inputs({"minuend": QUInt[3], "subtrahend": QUInt[3]})

negation_outputs = model.Negation(
    negation_params, in_wires={"arg": input_dict["subtrahend"]}
)
adder_outputs = model.Adder(
    adder_params,
    in_wires={
        "left_arg": input_dict["minuend"],
        "right_arg": negation_outputs["negated"],
    },
)

model.set_outputs({"difference": adder_outputs[adder_params.output_name]})