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 12 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 nodes/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
1 change: 1 addition & 0 deletions nodes/quantum-algorithms/grovers/grovers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function(RED) {
function GroversNode(config) {
RED.nodes.createNode(this, config);
this.name = config.name || 'Grovers';
const node = this;

this.on('input', async function(msg, send, done) {
let error = errors.validateGroversInput(msg);
Expand Down
45 changes: 25 additions & 20 deletions test/nodes/grovers_spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const groversNode = require('../../nodes/quantum-algorithms/grovers/grovers.js');
const testUtil = require('../test-util');
const nodeTestHelper = testUtil.nodeTestHelper;
const {FlowBuilder} = require('../flow-builder');
const errors = require('../../nodes/errors');
const assert = require('chai').assert;


describe('GroversNode', function() {
beforeEach(function(done) {
nodeTestHelper.startServer(done);
Expand All @@ -19,34 +20,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)=> {
const actualError = call.firstArg;
assert.strictEqual(actualError.message, errors.NOT_BIT_STRING);
done();
});

groversTestNode.receive({payload: '111111'});
groversTestNode.receive({payload: '111112'});
});
});
});
16 changes: 16 additions & 0 deletions test/nodes/measure_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ describe('MeasureNode', function() {

testUtil.commandExecuted(flow, command, 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);
});
});
19 changes: 19 additions & 0 deletions test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,27 @@ function commandExecuted(flowBuilder, command, done) {
});
}

function correctOutputReceived(flow, givenInput, expectedOutput, done) {
nodeTestHelper.load(flow.nodes, flow.flow, function() {
const inputNode = nodeTestHelper.getNode(flow.inputId);
const outputNode = nodeTestHelper.getNode(flow.outputId);
outputNode.on('input', function(msg) {
try {
assert.deepEqual(msg.payload, expectedOutput);
done();
} catch (err) {
done(err);
} finally {
shell.stop();
}
});
inputNode.receive(givenInput);
});
}

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