Skip to content
This repository has been archived by the owner on Sep 13, 2019. It is now read-only.

allow custom gates to be overwritten #76

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `@qiskit/qiskit-sim`: add prettyMatrix method to Gate class
- `@qiskit/qiskit-sim`: add gates to gates map in constructor
- `@qiskit/qiskit-sim`: remove unused addGate function in grammar.jison
- `@qiskit/qiskit-sim`: allow custom gates to be overwritten

## [0.9.0] - 2019-05-13

Expand Down
4 changes: 3 additions & 1 deletion packages/qiskit-sim/lib/gates.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ const phaseShift = shift =>
[[1, 0], [0, math.pow(math.e, math.multiply(math.i, math.PI / shift))]];

const gates = new Map();
let builtinGateNames;

class Gate {
constructor(name, matrix) {
this.name = name;
this.matrix = matrix;
if (!gates.has(name)) {
if (!builtinGateNames || !builtinGateNames.has(name)) {
gates.set(name, this);
}
}
Expand Down Expand Up @@ -103,6 +104,7 @@ Gate.cs = new Gate('cs', buildControlled(Gate.s));
Gate.cr2 = new Gate('cr2', buildControlled(Gate.r2));
Gate.cr4 = new Gate('cr4', buildControlled(Gate.r4));
Gate.cr8 = new Gate('cr8', buildControlled(Gate.r8));
builtinGateNames = new Set(gates.keys());

module.exports = {
Gate,
Expand Down
10 changes: 10 additions & 0 deletions packages/qiskit-sim/test/functional/gates.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ describe('sim:gates', () => {
const custom = new Gate('custom', [[1, 0], [0, 1]]);
assert.ok(gates.has(custom.name));
});

it('custom gate should be allowed to be overwritten', () => {
const custom = new Gate('custom', [[1, 0], [0, 1]]);
const newMatrix = [[1, 0],
[1, 1]];
const overwrite = new Gate(custom.name, newMatrix);
assert.ok(gates.has(overwrite.name));
assert.strictEqual(gates.get(custom.name).matrix, newMatrix);
});

});