@@ -20,7 +20,6 @@ var formatComplex2 = function(re, im, options) {
2020 var opt = JSON . parse ( JSON . stringify ( options ) ) ;
2121 opt . plusChar = "+" ;
2222 var sim = formatFloat ( im , opt ) ;
23-
2423 return sre + sim + ( options . iotaChar ? options . iotaChar : "i" ) ;
2524} ;
2625
@@ -5907,12 +5906,11 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {
59075906 }
59085907
59095908 // decompose
5910- if ( decompose ) {
5911- circuit = new QuantumCircuit ( ) ;
5912- circuit . load ( this . save ( true ) ) ;
5913- } else {
5914- circuit = this ;
5915- }
5909+ var obj = this . save ( decompose ) ;
5910+ var circuit = new QuantumCircuit ( ) ;
5911+ var tempCircuit = new QuantumCircuit ( ) ;
5912+ circuit . load ( obj ) ;
5913+ tempCircuit . load ( obj ) ;
59165914
59175915 var quirk = {
59185916 cols : [ ] ,
@@ -5926,6 +5924,57 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {
59265924 var gateLocations = { } ;
59275925 var customGateCircuit = new QuantumCircuit ( ) ;
59285926
5927+
5928+
5929+ var gatesToBeAdded = [ ] ;
5930+
5931+ if ( ! decompose ) {
5932+ var numCols = tempCircuit . numCols ( ) ;
5933+ for ( var column = numCols - 1 ; column >= 0 ; column -- ) {
5934+ for ( var wire = 0 ; wire < tempCircuit . numQubits ; wire ++ ) {
5935+ var gate = tempCircuit . gates [ wire ] [ column ] ;
5936+ if ( gate && gate . connector == 0 && ! tempCircuit . basicGates [ gate . name ] ) {
5937+ customDecomposedCircuit = tempCircuit . decomposeGateAt ( column , wire ) ;
5938+ var isDecomposeCustomCircuit = false ;
5939+ for ( var decomposedColumn = 0 ; decomposedColumn < customDecomposedCircuit . numCols ( ) ; decomposedColumn ++ ) {
5940+ for ( var decomposedWire = 0 ; decomposedWire < customDecomposedCircuit . numQubits ; decomposedWire ++ ) {
5941+ var gateInCustomCircuit = customDecomposedCircuit . getGateAt ( decomposedColumn , decomposedWire ) ;
5942+
5943+ if ( gateInCustomCircuit && gateInCustomCircuit . connector == 0 ) {
5944+ gatesToBeAdded . push ( gateInCustomCircuit ) ;
5945+ }
5946+
5947+ if ( gateInCustomCircuit ) {
5948+ var basicGateInCustomCircuit = customDecomposedCircuit . basicGates [ gateInCustomCircuit . name ] ;
5949+ if ( basicGateInCustomCircuit . exportInfo && ! basicGateInCustomCircuit . exportInfo . quirk ) {
5950+ isDecomposeCustomCircuit = true ;
5951+ }
5952+ }
5953+
5954+ if ( isDecomposeCustomCircuit ) {
5955+ circuit . gates . map ( function ( circuitGate , index ) {
5956+ circuitGate . map ( function ( gateToRemove ) {
5957+ if ( gateToRemove && gateToRemove . name == gate . name ) {
5958+ circuit . removeGate ( gateToRemove . id ) ;
5959+ }
5960+ } ) ;
5961+ } ) ;
5962+ } else {
5963+ gatesToBeAdded = [ ] ;
5964+ }
5965+ }
5966+ }
5967+ }
5968+ }
5969+ }
5970+ }
5971+
5972+ gatesToBeAdded . map ( function ( gateToAdd ) {
5973+ if ( gateToAdd ) {
5974+ circuit . insertGate ( gateToAdd . name , gateToAdd . column , gateToAdd . wires , gateToAdd . options ) ;
5975+ }
5976+ } ) ;
5977+
59295978 var numCols = circuit . numCols ( ) ;
59305979 for ( var column = 0 ; column < numCols ; column ++ ) {
59315980 var quirkColumn = [ ] ;
@@ -6035,7 +6084,6 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {
60356084 }
60366085 quirkColumn . push ( quirkName ) ;
60376086 } else {
6038- // wireRange = range(gate.wires.length);
60396087 var changeWires = JSON . stringify ( gateWires . sort ( ) ) == JSON . stringify ( wireRange ) ;
60406088 if ( ! changeWires && isWireInRange ( gate . wire , wireRange ) ) {
60416089 quirkColumn . push ( 1 ) ;
@@ -6067,6 +6115,7 @@ QuantumCircuit.prototype.exportQuirk = function(decompose) {
60676115 quirk . cols . push ( quirkColumn ) ;
60686116 }
60696117
6118+
60706119 // user defined gates
60716120 if ( ! decompose ) {
60726121 var usedGates = circuit . usedGates ( ) ;
0 commit comments