Skip to content
Open
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
193 changes: 118 additions & 75 deletions lib/quantum-circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5971,6 +5971,8 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {


var gatesToBeAdded = [];

var gateInformation = {};

if(!decompose) {
var numCols = tempCircuit.numCols();
Expand All @@ -5980,58 +5982,62 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {
if(gate && gate.connector == 0 && !tempCircuit.basicGates[gate.name]) {
customDecomposedCircuit = tempCircuit.decomposeGateAt(column, wire);
var isDecomposeCustomCircuit = false;
gateInformation[gate.name] = {info:[], isDecomposeCustomCircuit:null};
for(var decomposedColumn = 0; decomposedColumn < customDecomposedCircuit.numCols(); decomposedColumn++) {
for(var decomposedWire = 0; decomposedWire < customDecomposedCircuit.numQubits; decomposedWire++) {
var gateInCustomCircuit = customDecomposedCircuit.getGateAt(decomposedColumn, decomposedWire);

if(gateInCustomCircuit && gateInCustomCircuit.connector == 0){
gatesToBeAdded.push(gateInCustomCircuit);
}

if(gateInCustomCircuit){
var basicGateInCustomCircuit = customDecomposedCircuit.basicGates[gateInCustomCircuit.name];
if(basicGateInCustomCircuit.exportInfo && !basicGateInCustomCircuit.exportInfo.quirk){
isDecomposeCustomCircuit = true;
isDecomposeCustomCircuit = isDecomposeCustomCircuit || true;
}

if(gateInCustomCircuit.connector == 0){
gateInformation[gate.name].info.push({gateInCustomCircuit:gateInCustomCircuit, column:column});
gatesToBeAdded.push(gateInformation);
}
}
}

if(isDecomposeCustomCircuit){
circuit.gates.map(function(circuitGate, index){
circuitGate.map(function(gateToRemove){
if(gateToRemove && gateToRemove.name == gate.name){
circuit.removeGate(gateToRemove.id);
}
});
});
}else {
gatesToBeAdded = [];
gateInformation[gate.name].isDecomposeCustomCircuit= isDecomposeCustomCircuit;

}
}

if(isDecomposeCustomCircuit){
circuit.gates.map(function(circuitGate, index){
circuitGate.map(function(gateToRemove){
if(gateToRemove && gateToRemove.name == gate.name){
circuit.removeGate(gateToRemove.id);
}
});
});
}
}
}
}
}

gatesToBeAdded.map(function(gateToAdd){
if(gateToAdd){
circuit.insertGate(gateToAdd.name, gateToAdd.column, gateToAdd.wires, gateToAdd.options);
gateKeys = Object.keys(gateInformation);
for(var gateNumber = 0; gateNumber < gateKeys.length; gateNumber++){
gateKey = gateKeys[gateNumber];
if(gateInformation[gateKey].isDecomposeCustomCircuit){
for(var i = gateInformation[gateKey].info.length - 1; i >= 0; i--){
var gateInfo = gateInformation[gateKey].info[i];
var gateToAdd = gateInfo.gateInCustomCircuit;
circuit.insertGate(gateToAdd.name, gateInfo.column, gateToAdd.wires, gateToAdd.options);
}
}
});
}

var previousColumn = null;
var previousGate = null;
for(var column = 0; column < numCols; column++) {
for(var wire = 0; wire < circuit.numQubits; wire++) {
var gate = circuit.getGateAt(column, wire);
if(gate){
// if(previousGate){
// console.log(previousGate.name, gate.name, previousGate.connector, previousColumn, column)
// }
if(previousGate && previousGate.name != gate.name && previousGate.connector != 0 && previousColumn == column){
circuit.removeGate(gate.id);
circuit.insertGate(gate.name, gate.column + 1, gate.wires, gate.options);
// console.log(gate.name, previousColumn, column)
}
previousGate = gate;
previousColumn = column;
Expand Down Expand Up @@ -6087,71 +6093,108 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {

// Not directly supported by quirk
if(!quirkName) {
if(gate.connector == 0) {
quirkName = "~" + gate.name;
quirkName = "~" + gate.name;

if(basicGate.params.length && gate.options.params) {
basicGate.params.map(function(param){
angle = math.round(math.evaluate(gate.options.params[param], globalParams), 3);
quirkName += "_" + angle;
});
}

var alreadyDefined = quirk.gates.find(function(gt) {
return gt.id == quirkName;
if(basicGate.params.length && gate.options.params) {
basicGate.params.map(function(param){
angle = math.round(math.evaluate(gate.options.params[param], globalParams), 3);
quirkName += "_" + angle;
});
}

if(!alreadyDefined) {
var matrix = "";
matrix += "{";

var rawMatrix = self.getRawGate(basicGate, gate.options);
var matrix = "";
matrix += "{";

var targetWiresSorted = gateWires.sort();
var targetWires = [];
gate.wires.map(function(tWire) {
targetWires.push(targetWiresSorted.indexOf(tWire));
});
if(maxWire - minWire + 1 > gate.wires.length || gate.wires[0] > gate.wires[1]) {
gateMatrix = circuit.transformMatrix(maxWire - minWire + 1, rawMatrix, wireRange);
} else {
gateMatrix = rawMatrix;
}
var rawMatrix = self.getRawGate(basicGate, gate.options);

gateMatrix.map(function(row, rowIndex) {
if(rowIndex > 0) {
matrix += ",";
}
matrix += "{";
row.map(function(col, colIndex) {
if(colIndex > 0) {
matrix += ",";
}
var targetWiresSorted = gateWires.sort();
var targetWires = [];
gate.wires.map(function(tWire) {
targetWires.push(targetWiresSorted.indexOf(tWire));
});
if(maxWire - minWire + 1 > gate.wires.length || gate.wires[0] > gate.wires[1]) {
gateMatrix = circuit.transformMatrix(maxWire - minWire + 1, rawMatrix, targetWires);
} else {
gateMatrix = rawMatrix;
}

if(typeof col != "object") {
col = math.complex(col).toString();
} else {
col = col.toString();
}
gateMatrix.map(function(row, rowIndex) {
if(rowIndex > 0) {
matrix += ",";
}
matrix += "{";

row.map(function(col, colIndex) {
if(colIndex > 0) {
matrix += ",";
}

matrix += col;
});
matrix += "}";
});
matrix += "}";
if(typeof col != "object") {
col = math.complex(col).toString();
} else {
col = col.toString();
}

matrix += col;
});
matrix += "}";
});
matrix += "}";

if(gate.connector == 0) {
var alreadyDefined = quirk.gates.find(function(gt) {
return gt.id == quirkName;
});

if(!alreadyDefined) {
quirk.gates.push({
id: quirkName,
matrix: matrix
});
}
quirkColumn.push(quirkName);
} else {
var changeWires = JSON.stringify(gateWires.sort()) == JSON.stringify(wireRange);
if(!changeWires && isWireInRange(gate.wire, wireRange)){

var gatePresentInCol = false;
for(var columnElement = 0; columnElement < quirkColumn.length; columnElement++){
if(quirkColumn[columnElement] == quirkName){
gatePresentInCol = true;
}
}

if(!gatePresentInCol){
quirkColumn.push(quirkName);
}else {
quirkColumn.push(1);
}
} else {
if(wire < gate.wires[0]){
quirk.gates.push({
id: quirkName,
matrix: matrix
});
var alreadyDefined = quirk.gates.find(function(gt) {
return gt.id == quirkName;
});

if(!alreadyDefined) {
quirk.gates.push({
id: quirkName,
matrix: matrix
});
}

var gatePresentInCol = false;
for(var columnElement = 0; columnElement < quirkColumn.length; columnElement++){
if(quirkColumn[columnElement] == quirkName){
gatePresentInCol = true;
}
}

if(!gatePresentInCol){
quirkColumn.push(quirkName);
}else {
quirkColumn.push(1);
}
}
}
}
} else {
Expand Down