Skip to content

Commit

Permalink
Fixed #5 bug with classical control
Browse files Browse the repository at this point in the history
  • Loading branch information
perak committed Jan 25, 2019
1 parent 1acf7d7 commit aa346d0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 138 deletions.
121 changes: 53 additions & 68 deletions dist/quantum-circuit.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/quantum-circuit.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/quantum-circuit.min.js.map

Large diffs are not rendered by default.

119 changes: 52 additions & 67 deletions lib/quantum-circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,8 @@ QuantumCircuit.prototype.init = function(numQubits) {
this.customGates = {};
this.cregs = {};
this.collapsed = [];
this.measureFilter = {};
this.prob = [];
this.measureResetsQubit = false;

this.clear();
};
Expand Down Expand Up @@ -1053,7 +1053,6 @@ QuantumCircuit.prototype.resetState = function() {

// reset measurement
this.collapsed = [];
this.measureFilter = {};
this.prob = [];

// reset statistics
Expand Down Expand Up @@ -1657,6 +1656,32 @@ QuantumCircuit.prototype.applyTransform = function(U, qubits) {
this.stateBits = newStateBits;
};

QuantumCircuit.prototype.resetQubit = function(wire, value) {
var U = [
[0, 0],
[0, 0]
];

var prob = math.complex(0, 0);
for(var is in this.state) {
var i = parseInt(is);
var bit = math.pow(2, (this.numQubits - 1) - wire);
if(value ? !!(i & bit) : !(i & bit)) {
prob = math.add(prob, this.state[is]);
}
}

var mul = 1;
if(math.abs(prob)) {
mul = math.divide(1, prob);
}

value ? U[1][1] = mul : U[0][0] = mul;

this.collapsed = [];
this.prob = [];
this.applyTransform(U, [wire]);
};

QuantumCircuit.prototype.applyGate = function(gateName, wires, options) {
if(gateName == "measure") {
Expand All @@ -1666,8 +1691,9 @@ QuantumCircuit.prototype.applyGate = function(gateName, wires, options) {

var value = this.measure(wires[0], options.creg.name, options.creg.bit);

// add measure filter
this.measureFilter["" + wires[0]] = { value: value };
if(this.measureResetsQubit) {
this.resetQubit(wires[0], value);
}

return;
}
Expand All @@ -1685,13 +1711,6 @@ QuantumCircuit.prototype.applyGate = function(gateName, wires, options) {
this.prob = [];

this.applyTransform(rawGate, wires);

// remove measure filter from affected wires
for(var i = 0; i < wires.length; i++) {
if(this.measureFilter["" + wires[i]]) {
delete this.measureFilter["" + wires[i]];
}
}
};

QuantumCircuit.prototype.getRawGate = function(gate, options) {
Expand Down Expand Up @@ -2265,17 +2284,7 @@ QuantumCircuit.prototype.exportPyquil = function(comment, decompose, exportAsGat
});

// import MOVE, AND, OR if circuit has conditions
var gotConditions = false;
for(var column = 0; column < circuit.numCols(); column++) {
for(var wire = 0; wire < circuit.numQubits; wire++) {
var gate = circuit.getGateAt(column, wire);
if(gate && gate.connector == 0) {
if(gate.options && gate.options.condition && gate.options.condition.creg) {
gotConditions = true;
}
}
}
}
var gotConditions = circuit.gotClassicalControl();
if(gotConditions) {
if(importGates) {
importGates += ", ";
Expand Down Expand Up @@ -2705,18 +2714,7 @@ QuantumCircuit.prototype.exportQuil = function(comment, decompose, exportAsGateN
}
});

var gotConditions = false;
for(var column = 0; column < circuit.numCols(); column++) {
for(var wire = 0; wire < circuit.numQubits; wire++) {
var gate = circuit.getGateAt(column, wire);
if(gate && gate.connector == 0) {
if(gate.options && gate.options.condition && gate.options.condition.creg) {
gotConditions = true;
}
}
}
}

var gotConditions = this.gotClassicalControl();
var indent = "";
if(exportAsGateName) {
var params = "";
Expand Down Expand Up @@ -3874,6 +3872,8 @@ QuantumCircuit.prototype.partitionCircuit = function(partitionIndex) {
QuantumCircuit.prototype.run = function(initialValues, options) {
options = options || {};

this.measureResetsQubit = this.gotClassicalControl();

if(!options.continue) {
this.initState();
this.stats.duration = 0;
Expand Down Expand Up @@ -4077,6 +4077,20 @@ QuantumCircuit.prototype.print = function(onlyPossible) {
console.log(this.stateAsString(onlyPossible));
};

QuantumCircuit.prototype.gotClassicalControl = function() {
for(var column = 0; column < this.numCols(); column++) {
for(var wire = 0; wire < this.numQubits; wire++) {
var gate = this.getGateAt(column, wire);
if(gate && gate.connector == 0) {
if(gate.options && gate.options.condition && gate.options.condition.creg) {
return true;
}
}
}
}
return false;
};

QuantumCircuit.prototype.cregsAsString = function() {
var s = "";
for(var creg in this.cregs) {
Expand Down Expand Up @@ -4278,24 +4292,6 @@ QuantumCircuit.prototype.measureAll = function(force) {
return this.collapsed;
}

// measure filter ones bit map
var onesFilter = 0;
for(var sbit in this.measureFilter) {
var bit = (this.numQubits - 1) - parseInt(sbit);
if(this.measureFilter[sbit].value == 1) {
onesFilter += (1 << bit);
}
}

// measure filter zeroes bit map
var zeroesFilter = 0;
for(var sbit in this.measureFilter) {
var bit = (this.numQubits - 1) - parseInt(sbit);
if(this.measureFilter[sbit].value == 0) {
zeroesFilter += (1 << bit);
}
}

this.collapsed = [];
var randomRange = 0;
var maxChance = 0;
Expand All @@ -4313,22 +4309,11 @@ QuantumCircuit.prototype.measureAll = function(force) {
}

if(chance > maxChance || (chance == maxChance && (!this.collapsed.length || !Math.floor(Math.random() * (randomRange + 1)) ))) {
// filter
var i = parseInt(is);
var passesFilter = true;
if(onesFilter && ((i & onesFilter) != onesFilter)) {
passesFilter = false;
}
if(zeroesFilter && ((~i & zeroesFilter) != zeroesFilter)) {
passesFilter = false;
}

if(passesFilter) {
maxChance = chance;
this.collapsed = [];
for(var q = this.numQubits - 1; q >= 0; q--) {
this.collapsed.push(1 << q & i ? 1 : 0);
}
maxChance = chance;
this.collapsed = [];
for(var q = this.numQubits - 1; q >= 0; q--) {
this.collapsed.push(1 << q & i ? 1 : 0);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quantum-circuit",
"version": "0.9.51",
"version": "0.9.52",
"description": "Quantum Circuit Simulator",
"main": "lib/quantum-circuit.js",
"unpkg": "dist/quantum-circuit.min.js",
Expand Down

0 comments on commit aa346d0

Please sign in to comment.