Skip to content

Commit ade7ae1

Browse files
committed
Added matrix utils
1 parent 1ba672e commit ade7ae1

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

lib/quantum-circuit.js

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2850,9 +2850,16 @@ QuantumCircuit.prototype.evalMathString = function(str, vars) {
28502850
QuantumCircuit.prototype.parseMatrix = function(M, vars) {
28512851
var self = this;
28522852

2853+
var A = null;
2854+
if(typeof M == "string") {
2855+
A = self.evalMathString(M, vars);
2856+
} else {
2857+
A = M;
2858+
}
2859+
28532860
var res = [];
28542861

2855-
M.map(function(row, rowIndex) {
2862+
A.map(function(row, rowIndex) {
28562863
if(Array.isArray(row)) {
28572864
res.push([]);
28582865
row.map(function(val, colIndex) {
@@ -2982,6 +2989,95 @@ QuantumCircuit.prototype.stringifyMatrix = function(M, options) {
29822989
};
29832990

29842991

2992+
QuantumCircuit.prototype.matrixRe = function(U) {
2993+
var res = [];
2994+
for(var r = 0; r < U.length; r++) {
2995+
var row = U[r];
2996+
if(Array.isArray(row)) {
2997+
var newRow = [];
2998+
for(var c = 0; c < row.length; c++) {
2999+
if(typeof row[c] == "object") {
3000+
newRow.push(row[c].re);
3001+
} else {
3002+
newRow.push(row[c]);
3003+
}
3004+
}
3005+
res.push(newRow);
3006+
} else {
3007+
if(typeof row == "object") {
3008+
newRow.push(row.re);
3009+
} else {
3010+
newRow.push(row);
3011+
}
3012+
}
3013+
}
3014+
3015+
return res;
3016+
};
3017+
3018+
QuantumCircuit.prototype.matrixIm = function(U) {
3019+
var res = [];
3020+
for(var r = 0; r < U.length; r++) {
3021+
var row = U[r];
3022+
if(Array.isArray(row)) {
3023+
var newRow = [];
3024+
for(var c = 0; c < row.length; c++) {
3025+
if(typeof row[c] == "object") {
3026+
newRow.push(row[c].im);
3027+
} else {
3028+
newRow.push(0);
3029+
}
3030+
}
3031+
res.push(newRow);
3032+
} else {
3033+
if(typeof row == "object") {
3034+
newRow.push(row.im);
3035+
} else {
3036+
newRow.push(0);
3037+
}
3038+
}
3039+
}
3040+
3041+
return res;
3042+
};
3043+
3044+
QuantumCircuit.prototype.matrixAbs = function(U) {
3045+
var res = [];
3046+
for(var r = 0; r < U.length; r++) {
3047+
var row = U[r];
3048+
if(Array.isArray(row)) {
3049+
var newRow = [];
3050+
for(var c = 0; c < row.length; c++) {
3051+
newRow.push(math.abs(row[c]));
3052+
}
3053+
res.push(newRow);
3054+
} else {
3055+
res.push(math.abs(row));
3056+
}
3057+
}
3058+
3059+
return res;
3060+
};
3061+
3062+
QuantumCircuit.prototype.matrixArg = function(U) {
3063+
var res = [];
3064+
for(var r = 0; r < U.length; r++) {
3065+
var row = U[r];
3066+
if(Array.isArray(row)) {
3067+
var newRow = [];
3068+
for(var c = 0; c < row.length; c++) {
3069+
newRow.push(math.arg(row[c]));
3070+
}
3071+
res.push(newRow);
3072+
} else {
3073+
res.push(math.arg(row));
3074+
}
3075+
}
3076+
3077+
return res;
3078+
};
3079+
3080+
29853081
//
29863082
// combineList is array of objects:
29873083
//

0 commit comments

Comments
 (0)