@@ -9250,19 +9250,27 @@ QuantumCircuit.prototype.exportToAQASM = function(options, isExportPyAQASM, expo
92509250 var hybrid = ! ! isExportPyAQASM ? options . hybrid : false ;
92519251
92529252 var circuit = null ;
9253+ var tempCircuit = null ;
92539254
92549255 shots = shots || 1024 ;
92559256
92569257 if ( typeof hybrid == "undefined" ) {
92579258 hybrid = this . options ? ! ! this . options . hybrid : false ;
92589259 }
92599260
9261+ var obj = null ;
92609262 // decompose
92619263 if ( decompose ) {
92629264 circuit = new QuantumCircuit ( ) ;
9263- circuit . load ( this . save ( true ) ) ;
9265+ tempCircuit = new QuantumCircuit ( ) ;
9266+ obj = this . save ( true ) ;
9267+ circuit . load ( obj ) ;
9268+ tempCircuit . load ( obj ) ;
92649269 } else {
92659270 circuit = this ;
9271+ obj = this . save ( false ) ;
9272+ tempCircuit = new QuantumCircuit ( ) ;
9273+ tempCircuit . load ( obj ) ;
92669274 }
92679275
92689276 var mathToStringHandler = function ( node , options ) {
@@ -9316,6 +9324,44 @@ QuantumCircuit.prototype.exportToAQASM = function(options, isExportPyAQASM, expo
93169324
93179325 indentDepth = indentDepth || 0 ;
93189326
9327+ var gatesToBeAdded = [ ] ;
9328+ if ( ! isExportPyAQASM ) {
9329+ if ( ! decompose ) {
9330+ var numCols = tempCircuit . numCols ( ) ;
9331+ for ( var column = numCols - 1 ; column >= 0 ; column -- ) {
9332+ for ( var wire = 0 ; wire < tempCircuit . numQubits ; wire ++ ) {
9333+ var gate = tempCircuit . gates [ wire ] [ column ] ;
9334+ if ( gate && gate . connector == 0 && ! tempCircuit . basicGates [ gate . name ] ) {
9335+ customDecomposedCircuit = tempCircuit . decomposeGateAt ( column , wire ) ;
9336+ var isDecomposeCustomCircuit = false ;
9337+ for ( var decomposedColumn = 0 ; decomposedColumn < customDecomposedCircuit . numCols ( ) ; decomposedColumn ++ ) {
9338+ for ( var decomposedWire = 0 ; decomposedWire < customDecomposedCircuit . numQubits ; decomposedWire ++ ) {
9339+ var gateInCustomCircuit = customDecomposedCircuit . getGateAt ( decomposedColumn , decomposedWire ) ;
9340+
9341+ if ( gateInCustomCircuit && gateInCustomCircuit . connector == 0 ) {
9342+ gatesToBeAdded . push ( gateInCustomCircuit ) ;
9343+ }
9344+
9345+ circuit . gates . map ( function ( circuitGate , index ) {
9346+ circuitGate . map ( function ( gateToRemove ) {
9347+ if ( gateToRemove && gateToRemove . name == gate . name ) {
9348+ circuit . removeGate ( gateToRemove . id ) ;
9349+ }
9350+ } ) ;
9351+ } ) ;
9352+ }
9353+ }
9354+ }
9355+ }
9356+ }
9357+ }
9358+ gatesToBeAdded . map ( function ( gateToAdd ) {
9359+ if ( gateToAdd ) {
9360+ circuit . insertGate ( gateToAdd . name , gateToAdd . column , gateToAdd . wires , gateToAdd . options ) ;
9361+ }
9362+ } ) ;
9363+ }
9364+
93199365 var aqasm = "" ;
93209366 var indent = getIndent ( indentDepth ) ;
93219367 var usedGates = circuit . usedGates ( ) ;
0 commit comments