node-red-quantum / node-red-contrib-quantum

Quantum computing functionality for Node-RED
https://node-red-quantum.github.io/
Apache License 2.0
16 stars 2 forks source link

Register circuit fails when injecting input a second time #126

Closed ttoktassynov closed 3 years ago

ttoktassynov commented 3 years ago

Bug Description

When injecting input to the register only circuit a second time in a row, after first successful input, we receive following error of Quantum Register:

(node:87745) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:87745) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:87745) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined
    at ClassicalRegisterNode._inputCallback (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/nodes/quantum/classical-register/classical-register.js:45:19)
    at /Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/nodes/Node.js:203:26
    at Object.trigger (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/hooks.js:113:9)
    at ClassicalRegisterNode.Node._emitInput (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/nodes/Node.js:195:11)
    at ClassicalRegisterNode.Node.emit (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/nodes/Node.js:179:25)
    at ClassicalRegisterNode.Node.receive (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/nodes/Node.js:476:10)
    at Immediate.<anonymous> (/Users/ttoktassynov/projects/nodejs_projects/node-red-contrib-quantum/node_modules/@node-red/runtime/lib/flows/Flow.js:679:52)
    at processImmediate (internal/timers.js:461:21)
(node:87745) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Most likely it has to do with state management functionanility.

Steps to Reproduce Bug

Draw this circuit: Screen Shot 2021-09-07 at 21 50 14

or simply import this json

Expected Behaviour & Actual Behaviour

Additional Information

Log file can be found here.

System Information

louislefevre commented 3 years ago

Seems to be caused by the registerArr variable in quantum-register.js. On first execution, its initial value is []. On second execution, its initial value is undefined.

Also, it's worth noting that this behaviour happens in the same loop:

louislefevre commented 3 years ago

Okay so I think I figured out the cause. This is the flow of execution:

A fix I've just quickly tested that seems to work is to replace all instances of state.del('registers') with state.setPersistent('registers', []). However, I'm not sure if this will have any unforeseen consequences, since registers is no longer removed completely like it was before.

ttoktassynov commented 3 years ago

Maybe try to implement in different branch and see if tests pass?

louislefevre commented 3 years ago

Maybe try to implement in different branch and see if tests pass?

The tests still pass locally, but since we don't have tests for all scenarios I'm still unsure as I don't know the reason for deleting 'registers' from the state in the first place.