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

Local simulator received same qubit twice when running flow via flow-builder #109

Closed ttoktassynov closed 3 years ago

ttoktassynov commented 3 years ago

Bug Description

Local simulator received same qubit twice when running flow via flow-builder.

Steps to Reproduce Bug

In this test case

 xit('should return correct output for register only circuit', function(done) {
    flow.add('quantum-circuit', 'qc', [['qr'], ['cr']],
      {structure: 'registers', outputs: '2', qbitsreg: '1', cbitsreg: '1'});
    flow.add('classical-register', 'cr', [[]], {classicalBits:'2'});
    flow.add('quantum-register', 'qr', [['ng', 'm1']], {outputs:2});
    flow.add('not-gate', 'ng', [['m2']]);
    flow.add('measure', 'm1', [['si']], {selectedBit: 0});
    flow.add('measure', 'm2', [['si']], {selectedBit: 1});
    flow.add('local-simulator', 'si', [['out']], {shots: '1'});
    flow.addOutput('out');

    const givenInput = {payload: ''};
    const expectedOutput = {'10': 1};
    testUtil.correctOutputReceived(flow, givenInput, expectedOutput, done);
  });

we receive following error:

const SAME_QUBIT_RECEIVED_TWICE =
'Please connect the right number of qubits to the node. For circuit output nodes, ' +
'all qubits should be connected as input. There should be only 1 instance of each qubit at all times in the circuit.';

Expected Behaviour & Actual Behaviour

Expected output: Screen Shot 2021-08-30 at 22 29 11

Additional Information

System Information

louislefevre commented 3 years ago

I just ran this test but I'm not getting the error you said, the test is timing out instead.

louislefevre commented 3 years ago

This is what the log file is reporting for me:

[2021-08-30 23:50:28] [TRACE] [qc] Initialised quantum circuit
[2021-08-30 23:50:28] [TRACE] [cr] Initialised classical register
[2021-08-30 23:50:28] [TRACE] [qr] Initialised quantum register
[2021-08-30 23:50:28] [TRACE] [ng] Initialised not gate
[2021-08-30 23:50:28] [TRACE] [m1] Initialised measure
[2021-08-30 23:50:28] [TRACE] [m2] Initialised measure
[2021-08-30 23:50:28] [TRACE] [si] Initialised local simulator
[2021-08-30 23:50:28] [TRACE] [qc] Quantum circuit received input
[2021-08-30 23:50:29] [TRACE] [qc] Executed quantum circuit command
[2021-08-30 23:50:29] [TRACE] [qr] Quantum register received input
[2021-08-30 23:50:29] [TRACE] [cr] Classical register received input
[2021-08-30 23:50:29] [TRACE] [qr] Executed quantum register command
[2021-08-30 23:50:29] [TRACE] [qr] Quantum register waiting for circuit to be ready
[2021-08-30 23:50:29] [TRACE] [ng] Not gate received input
[2021-08-30 23:50:29] [TRACE] [m1] Measure received input
[2021-08-30 23:50:29] [TRACE] [ng] Executed not gate command
[2021-08-30 23:50:29] [ERROR] [ng] Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'qc' is not defined
[2021-08-30 23:50:29] [TRACE] [m1] Executed measure command
[2021-08-30 23:50:29] [ERROR] [m1] Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'qc' is not defined
ttoktassynov commented 3 years ago

This is an error due to wrong persistence of quantum circuit data in the state manager. After #92 it should be gone.

But the reason why it would timeout is because when input will come to local simulator node, than error will be thrown. You can console log in local simulator js file to make sure. The test will just timeout, since last output node in correctOutputReceived function will never receive any message

ttoktassynov commented 3 years ago

@Theo-Reignier can think of reason why such flow creation might result in ‘same qubit coming twice’ situation? Although regular flow creation via editor works with no problem .

Theo-Reignier commented 3 years ago

@Theo-Reignier can think of reason why such flow creation might result in ‘same qubit coming twice’ situation? Although regular flow creation via editor works with no problem .

I don't know, the circuit illustrated above should not raise any error. Maybe the correctOutputReceived function is not working properly.

ttoktassynov commented 3 years ago

correctOutputReceived just loads the flow, injects the input to the first node and catches the output from the last output. Shouldn't any problems there.

louislefevre commented 3 years ago

@ttoktassynov Could you paste your latest log file (from the logs/ directory) when running just this test please? You can use the command export NODE_ENV=dev && npx mocha test/nodes/local-simulator_spec.js -g 'should return correct output for register only circuit' --timeout 10000

ttoktassynov commented 3 years ago

Pardon, guys. There was a type in the line where I added a quantum register to the flow:

flow.add('quantum-register', 'qr', [['ng', 'm1']], {outputs:2});

Forgot to wrap 'ng' and 'm1' nodes into brackets correctly. Everything now works fine.