Skip to content

Integrating External Programs

Theo Reignier edited this page Aug 27, 2021 · 7 revisions

This section provides a tutorial on how to integrate your quantum flows in programs external to Node-RED. You can achieve this by using Node-Red's built-in 'Http In', 'Catch', 'Function', and 'Http Response' nodes.

Set up an HTTP endpoint

  1. First drag and drop a 'Http In' node to your flow, set the HTTP method to post.
  2. Define the URL endpoint from which you wish to trigger the quantum circuit.
  3. Connect the 'Http In' node to the start of your flow ('Quantum Circuit' node for example).
  4. Drag and drop a 'Http Response' node, add a header Content-Type and set it to application/json.
  5. Connect the 'Http Response' node with the output of 'Local Simulator' node or 'IBM Quantum System' node, the execution result will then be sent back in JSON format to the requestor. A successful response for a 2-qubits circuit will look like this:
{
    "00": 55,
    "01": 43,
    "10": 22,
    "11": 80
}

Trigger the quantum flow

To trigger the quantum flow, send an HTTP post request to http[s]://[host-address-of-node-red-instance]/[URL endpoint]. The request can be:

  • {payload: {binaryString: [binary string] }} → To specify the initial state of the qubits. The binary string must be made of bits (0 or 1) and have a length equal to the number of qubits in the quantum circuit. The first qubit will be initialised according to the first bit of the string etc.
  • [anything] → To simply run the circuit and receiving the output.

If the Node-Red instance was deployed on a public cloud, make sure to check the firewall configurations and set up a reverse proxy server if necessary to bypass CORS regulations to the API endpoint.

Error handling

You might want to add another block of nodes to do the error handling tasks in case of any failed execution. Otherwise, error messages will simply be returned to Node-RED, not to the requester.

  1. Drag and drop a 'Catch' node, and set it to catch errors from all nodes.
  2. Drag and drop a 'Function' node, paste the following code in the on Message tab:
    msg.payload.message=msg.error.message;
    msg.payload.source=msg.error.source;
    return msg;
    
  3. Drag and drop another 'Http Response' node and set the status code to 500 which represents internal server error.
  4. Connect the above nodes in following order: CatchFunctionHttp Response.

This will return the error message and the node information which caused the error to the requester. An example error response will look like this:

{
    "binaryString": "01010",
    "message": "Error: Binary string length does not match. Expect: 4, actual: 5",
    "source": {
        "id": "de819baa.357618",
        "type": "quantum-circuit",
        "name": "",
        "count": 1
    }
}