Skip to content

Commit 1c519d2

Browse files
committed
Improved splitIntoBlocks
1 parent 371066a commit 1c519d2

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

lib/quantum-circuit.js

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4426,7 +4426,8 @@ QuantumCircuit.prototype.validCustomGateName = function(baseName) {
44264426
//
44274427
// Options:
44284428
// {
4429-
// flexibleBlockSize: bool // Allow larger blocks for gates with more qubits than block size
4429+
// flexibleBlockSize: bool // Allow larger blocks for gates with more qubits than block size
4430+
// verticalOnly: bool // Don't extend blocks horizontally
44304431
// }
44314432
//
44324433

@@ -4446,9 +4447,8 @@ QuantumCircuit.prototype.splitIntoBlocks = function(blockSize, options) {
44464447
for(var column = 0; column < numCols; column++) {
44474448
for(var wire = 0; wire < decomposed.numQubits; wire++) {
44484449
var gate = decomposed.getGateAt(column, wire);
4449-
if(gate && gate.connector == 0) {
4450+
if(gate && gate.connector == 0 && !gate.addedToBlock) {
44504451
var gateDef = this.basicGates[gate.name];
4451-
44524452
var currentCondition = gate.options && gate.options.condition ? JSON.stringify(gate.options.condition) : "{}";
44534453
var currentIsUnitary = gateDef && !!(gateDef.matrix && gateDef.matrix.length);
44544454

@@ -4480,8 +4480,78 @@ QuantumCircuit.prototype.splitIntoBlocks = function(blockSize, options) {
44804480
currentBlock.gates.push(gate);
44814481
}
44824482

4483+
gate.wires.map(function(ww) {
4484+
decomposed.gates[ww][column].addedToBlock = true;
4485+
});
4486+
44834487
prevCondition = currentCondition;
44844488
prevIsUnitary = currentIsUnitary;
4489+
4490+
if(!options.verticalOnly) {
4491+
// try to extend the block horizontally
4492+
var extColumn = column + 1;
4493+
var extWires = JSON.parse(JSON.stringify(currentBlock.wires));
4494+
extWires.sort();
4495+
while(extColumn < numCols && extWires.length) {
4496+
extWire = extWires[0];
4497+
4498+
while(extWires.length && extWire <= extWires[extWires.length - 1]) {
4499+
var extGate = decomposed.getGateAt(extColumn, extWire);
4500+
if(extGate && !extGate.addedToBlock) {
4501+
var extGateDef = this.basicGates[extGate.name];
4502+
var extCondition = extGate.options && extGate.options.condition ? JSON.stringify(extGate.options.condition) : "{}";
4503+
var extIsUnitary = extGateDef && !!(extGateDef.matrix && extGateDef.matrix.length);
4504+
4505+
//
4506+
// Check if gate is valid for block
4507+
//
4508+
var gateValid = extIsUnitary && (extCondition == currentCondition);
4509+
4510+
// Check if all gate's wires are inside block
4511+
if(gateValid) {
4512+
for(var ww = 0; ww < extGate.wires.length; ww++) {
4513+
if(extWires.indexOf(extGate.wires[ww]) < 0) {
4514+
gateValid = false;
4515+
break;
4516+
}
4517+
}
4518+
}
4519+
4520+
if(gateValid) {
4521+
// add gate to block
4522+
currentBlock.gates.push(extGate);
4523+
4524+
extGate.wires.map(function(ww) {
4525+
decomposed.gates[ww][extColumn].addedToBlock = true;
4526+
});
4527+
} else {
4528+
// remove gate's wires from scan
4529+
var validExtWires = [];
4530+
for(var ww = 0; ww < extWires.length; ww++) {
4531+
var extW = extWires[ww];
4532+
if(extGate.wires.indexOf(extW) < 0) {
4533+
validExtWires.push(extW);
4534+
}
4535+
}
4536+
extWires = validExtWires;
4537+
}
4538+
}
4539+
// goto next valid wire
4540+
if(extWires.length) {
4541+
var newExtWire = extWires[extWires.length - 1] + 1;
4542+
for(var ww = extWires.length - 1; ww >= 0; ww--) {
4543+
var extW = extWires[ww];
4544+
if(extW > extWire) {
4545+
newExtWire = extW;
4546+
}
4547+
}
4548+
extWire = newExtWire;
4549+
}
4550+
4551+
}
4552+
extColumn++;
4553+
}
4554+
}
44854555
}
44864556
}
44874557
}

0 commit comments

Comments
 (0)