Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for Grovers, Measure nodes #91

Merged
merged 16 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions quantum/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ module.exports = {
QUBITS_FROM_DIFFERENT_CIRCUITS,
SAME_QUBIT_RECEIVED_TWICE,
BLOCH_SPHERE_WITH_MEASUREMENT,
NOT_BIT_STRING,
validateQubitInput,
validateRegisterInput,
validateQubitsFromSameCircuit,
Expand Down
3 changes: 2 additions & 1 deletion quantum/nodes/grovers/grovers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = function(RED) {
this.on('input', async function(msg, send, done) {
let error = errors.validateGroversInput(msg);
if (error) {
done(error);
this.error(error.message);
done();
ttoktassynov marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
1 change: 1 addition & 0 deletions quantum/nodes/quantum-circuit/quantum-circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = function(RED) {
this.qbitsreg = parseInt(config.qbitsreg);
this.cbitsreg = parseInt(config.cbitsreg);
this.outputs = parseInt(config.outputs);
this.shell = shell;
const state = stateManager.newState(this.id);
const node = this;

Expand Down
1 change: 1 addition & 0 deletions test/flow-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const NODES = {
'swap': require('../quantum/nodes/swap/swap.js'),
'toffoli-gate': require('../quantum/nodes/toffoli-gate/toffoli-gate.js'),
'unitary-gate': require('../quantum/nodes/unitary-gate/unitary-gate.js'),
'grovers': require('../quantum/nodes/grovers/grovers')
};

class FlowBuilder {
Expand Down
44 changes: 25 additions & 19 deletions test/nodes/grovers_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const groversNode = require('../../quantum/nodes/grovers/grovers.js');
const testUtil = require('../test-util');
const nodeTestHelper = testUtil.nodeTestHelper;
const assert = require('chai').assert;
const {FlowBuilder} = require('../flow-builder');
const errors = require('../../quantum/errors');


describe('GroversNode', function() {
Expand All @@ -19,34 +21,38 @@ describe('GroversNode', function() {
});

it('default name outputs correctly', function(done) {
let flow = [{id: 'groversNode', type: 'grovers', wires: []}];
nodeTestHelper.load(groversNode, flow, function() {
flow = new FlowBuilder();
flow.add('grovers', 'groversNode', []);

nodeTestHelper.load(flow.nodes, flow.flow, function() {
let groversTestNode = nodeTestHelper.getNode('groversNode');
groversTestNode.should.have.property('name', 'Grovers');
groversTestNode.should.have.property('name', 'grovers');
done();
});
});

it('return success output on valid input', function(done) {
let flow = [{id: 'groversNode', type: 'grovers', wires: [['helperNode']]},
{id: 'helperNode', type: 'helper'}];
flow = new FlowBuilder();
flow.add('grovers', 'n1', [['n2']]);
flow.addOutput('n2');

const givenInput = {payload: '111111'};
const expectedOutput = { topMeasurement: '111111', iterationsNum: 6};

testUtil.correctOutputReceived(flow, givenInput, expectedOutput, done);
});

it('should fail on invalid input', function(done) {
flow = new FlowBuilder();
flow.add('grovers', 'groversNode', []);

nodeTestHelper.load(groversNode, flow, function() {
nodeTestHelper.load(flow.nodes, flow.flow, function() {
let groversTestNode = nodeTestHelper.getNode('groversNode');
let helperNode = nodeTestHelper.getNode('helperNode');

helperNode.on('input', function(msg) {
const expectedPayload = { topMeasurement: '111111', iterationsNum: 6};
try {
assert.strictEqual(msg.payload.topMeasurement, expectedPayload.topMeasurement);
assert.strictEqual(msg.payload.iterationsNum, expectedPayload.iterationsNum);
done();
} catch (err) {
done(err);
}
groversTestNode.on('call:error', call => {
call.should.be.calledWithExactly(errors.NOT_BIT_STRING);
done();
});

groversTestNode.receive({payload: '111111'});
groversTestNode.receive({payload: '111112'});
});
});
});
17 changes: 17 additions & 0 deletions test/nodes/measure_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const measureNode = require('../../quantum/nodes/measure/measure.js');
const testUtil = require('../test-util');
const nodeTestHelper = testUtil.nodeTestHelper;
const {FlowBuilder} = require('../flow-builder');


describe('MeasureNode', function() {
Expand All @@ -16,4 +17,20 @@ describe('MeasureNode', function() {
it('load node', function(done) {
testUtil.isLoaded(measureNode, 'measure', done);
});

it('should return correct output', function(done) {
let flow = new FlowBuilder();

flow.add('quantum-circuit', 'n0', [['n1']],
{structure: 'qubits', outputs: '1', qbitsreg: '1', cbitsreg: '1'});
flow.add('not-gate', 'n1', [['n2']]);
flow.add('measure', 'n2', [['n3']], {selectedBit: '0'});
flow.add('local-simulator', 'n3', [['n4']], {shots: 1});
flow.addOutput('n4');

const givenInput = 'dummy input';
const expectedOutput = {1: 1};

testUtil.correctOutputReceived(flow, givenInput, expectedOutput, done);
});
});
25 changes: 25 additions & 0 deletions test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,32 @@ function isLoaded(node, nodeName, done) {
});
}

function correctOutputReceived(flow, givenInput, expectedOutput, done) {
nodeTestHelper.load(flow.nodes, flow.flow, function() {
const inputNodeId = flow.flow[0].id;
const inputNode = nodeTestHelper.getNode(inputNodeId);

const outputNodeId = flow.flow[flow.flow.length - 1].id;
const outputNode = nodeTestHelper.getNode(outputNodeId);

outputNode.on('input', function(msg){
try {
assert.deepEqual(msg.payload, expectedOutput);
done();
}
catch(err){
done(err);
}
finally{
inputNode.shell.stop();
ttoktassynov marked this conversation as resolved.
Show resolved Hide resolved
}
});
inputNode.receive(givenInput);
});
}

module.exports = {
nodeTestHelper,
correctOutputReceived,
isLoaded,
};