Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
perak committed Feb 13, 2023
1 parent 0813d40 commit 7dd1f05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added lib/.DS_Store
Binary file not shown.
42 changes: 33 additions & 9 deletions lib/quantum-circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2937,12 +2937,12 @@ QuantumCircuit.prototype.matrixHasComplexElement = function(M) {
for(var c = 0; c < row.length; c++) {
var cell = row[c];

if(cell instanceof math.Complex) {
if(cell instanceof math.Complex || ((typeof cell == "object") && ((cell.mathjs && cell.mathjs == "Complex") || (cell.type && cell.type == "Complex")))) {
return true;
}
}
} else {
if(row instanceof math.Complex) {
if(row instanceof math.Complex || ((typeof row == "object") && ((row.mathjs && row.mathjs == "Complex") || (row.type && row.type == "Complex")))) {
return true;
}
}
Expand Down Expand Up @@ -3420,6 +3420,7 @@ QuantumCircuit.prototype.isEmptyPlace = function(col, wires, usingCregs) {
for(var wire = minWire; wire <= maxWire; wire++) {
if(!this.isEmptyCell(col, wire)) {
allEmpty = false;
break;
}
}

Expand All @@ -3444,6 +3445,7 @@ QuantumCircuit.prototype.lastNonEmptyPlace = function(wires, usingCregs) {
for(var wire = minWire; wire <= maxWire; wire++) {
if(!this.isEmptyCell(col, wire)) {
allEmpty = false;
break;
}
}
}
Expand Down Expand Up @@ -4121,15 +4123,20 @@ QuantumCircuit.prototype.chanceMap = function() {
};


function binStr(i, len) {
var bin = i.toString(2);
function binStr(n, len) {
var bin = n.toString(2);
while(bin.length < len) {
bin = "0" + bin;
}
return bin;
}


function reverseBitwise(n, len) {
return parseInt(binStr(n, len).split("").reverse().join(""), 2);
}


QuantumCircuit.prototype.resetQubit = function(wire, value) {
var U = [
[0, 0],
Expand Down Expand Up @@ -7455,6 +7462,7 @@ QuantumCircuit.prototype.exportToSVG = function(options) {
var centerX = gateX + (options.cellWidth / 2);

var paramsStr = "";
var paramsCount = 0;
for(var paramName in gate.options.params) {
if(paramsStr) {
paramsStr += ", ";
Expand All @@ -7470,6 +7478,11 @@ QuantumCircuit.prototype.exportToSVG = function(options) {
}

paramsStr += paramVal;
paramsCount++;
}

if(paramsStr.length > 26) {
paramsStr = "(" + paramsCount + " params)";
}
svg += "<text class=\"qc-gate-params\" x=\"" + centerX + "\" y=\"" + (gateY + options.cellHeight + options.paramTextHeight) + "\" dominant-baseline=\"hanging\" text-anchor=\"middle\" font-size=\"75%\">" + paramsStr + "</text>";
}
Expand Down Expand Up @@ -7696,7 +7709,7 @@ QuantumCircuit.prototype.exportToSVG = function(options) {
if(options.customGate) {
initSymbol = qubitLetter(wire, numRows);
}
svg += "<text class=\"qc-wire-init\" x=\"0\" y=\"" + wireY + "\" dominant-baseline=\"middle\" text-anchor=\"start\">|" + initSymbol + "&#x027E9;</text>";
svg += "<text class=\"qc-wire-init\" x=\"0\" y=\"" + wireY + "\" dominant-baseline=\"middle\" text-anchor=\"start\">|" + initSymbol + "</text>";
svg += "<line class=\"qc-wire\" x1=\"" + options.wireMargin + "\" x2=\"" + totalWireWidth + "\" y1=\"" + wireY + "\" y2=\"" + wireY + "\" stroke=\"" + options.wireColor + "\" stroke-width=\"" + options.wireWidth + "\" />";
svg += "<text class=\"qc-wire-label\" x=\"" + options.wireMargin + "\" y=\"" + (wireY - (options.wireTextHeight*2)) + "\" dominant-baseline=\"hanging\" text-anchor=\"start\" font-size=\"75%\">q" + wire + "</text>";
}
Expand Down Expand Up @@ -12436,7 +12449,7 @@ QuantumCircuit.prototype.continue = function() {
});
};

QuantumCircuit.prototype.stateAsArray = function(onlyPossible, skipItems, blockSize) {
QuantumCircuit.prototype.stateAsArray = function(onlyPossible, skipItems, blockSize, reverseBits) {
var state = [];

var numAmplitudes = this.numAmplitudes();
Expand All @@ -12446,7 +12459,13 @@ QuantumCircuit.prototype.stateAsArray = function(onlyPossible, skipItems, blockS

var count = 0;
for(var i = 0; i < numAmplitudes; i++) {
var amplitude = math.round(this.state[i] || math.complex(0, 0), 14);
var ampIndex = i;
if(reverseBits) {
ampIndex = reverseBitwise(i, this.numQubits);
}

var amplitude = math.round(this.state[ampIndex] || math.complex(0, 0), 14);

if(!onlyPossible || (amplitude.re || amplitude.im)) {
if(count >= skipItems) {
var indexBinStr = i.toString(2);
Expand Down Expand Up @@ -12485,7 +12504,7 @@ QuantumCircuit.prototype.stateAsArray = function(onlyPossible, skipItems, blockS
return state;
};

QuantumCircuit.prototype.stateAsSimpleArray = function() {
QuantumCircuit.prototype.stateAsSimpleArray = function(reverseBits) {

var numAmplitudes = this.numAmplitudes();
if(!this.state) {
Expand All @@ -12494,7 +12513,12 @@ QuantumCircuit.prototype.stateAsSimpleArray = function() {

var state = [];
for(var i = 0; i < numAmplitudes; i++) {
state.push(math.round(this.state[i] || math.complex(0, 0), 14));
var ampIndex = i;
if(reverseBits) {
ampIndex = reverseBitwise(i, this.numQubits);
}

state.push(math.round(this.state[ampIndex] || math.complex(0, 0), 14));
}
return state;
};
Expand Down

0 comments on commit 7dd1f05

Please sign in to comment.