@@ -2994,7 +2994,7 @@ QuantumCircuit.prototype.stringifyMatrix = function(M, options) {
29942994 if ( el instanceof math . Complex ) {
29952995 str += self . formatComplex ( el , options ) ;
29962996 } else {
2997- if ( ( typeof el == "object" ) && el . mathjs ) {
2997+ if ( ( typeof el == "object" ) && ( ( el . mathjs && el . mathjs == "Complex" ) || ( el . type && el . type == "Complex" ) ) ) {
29982998 str += formatComplex2 ( el . re , el . im , options ) ;
29992999 } else {
30003000 if ( typeof el == "string" ) {
@@ -3015,7 +3015,7 @@ QuantumCircuit.prototype.stringifyMatrix = function(M, options) {
30153015 if ( row instanceof math . Complex ) {
30163016 str += self . formatComplex ( row , options ) ;
30173017 } else {
3018- if ( ( typeof el == "object" ) && el . mathjs ) {
3018+ if ( ( typeof row == "object" ) && ( ( row . mathjs && row . mathjs == "Complex" ) || ( row . type && row . type == "Complex" ) ) ) {
30193019 str += formatComplex2 ( el . re , el . im , options ) ;
30203020 } else {
30213021 if ( typeof row == "string" ) {
@@ -3643,6 +3643,11 @@ QuantumCircuit.prototype.appendCircuit = function(circuit, pack) {
36433643 for ( var wire = 0 ; wire < circuit . numQubits ; wire ++ ) {
36443644 var gate = circuit . getGateAt ( column , wire ) ;
36453645 if ( gate && gate . connector == 0 ) {
3646+
3647+ if ( ! this . basicGates [ gate . name ] && circuit . customGates [ gate . name ] ) {
3648+ this . registerGate ( gate . name , circuit . customGates [ gate . name ] ) ;
3649+ }
3650+
36463651 this . addGate ( gate . name , column + colOffset , gate . wires , gate . options ) ;
36473652 }
36483653 }
@@ -4782,6 +4787,7 @@ QuantumCircuit.prototype.splitIntoBlocks = function(blockSize, options) {
47824787 this . load ( circuit . save ( false ) ) ;
47834788} ;
47844789
4790+
47854791QuantumCircuit . prototype . usedGates = function ( options ) {
47864792 options = options || { } ;
47874793
@@ -4822,6 +4828,56 @@ QuantumCircuit.prototype.usedGates = function(options) {
48224828 return used ;
48234829} ;
48244830
4831+
4832+ QuantumCircuit . prototype . countOps = function ( obj , options ) {
4833+ options = options || { } ;
4834+ options . shallow = options . shallow || false ;
4835+
4836+ if ( ! obj ) {
4837+ obj = this ;
4838+ }
4839+
4840+ var ops = { } ;
4841+ for ( var wire = 0 ; wire < obj . gates . length ; wire ++ ) {
4842+ for ( var col = 0 ; col < obj . gates [ wire ] . length ; col ++ ) {
4843+ var gate = obj . gates [ wire ] [ col ] ;
4844+ if ( gate && gate . connector == 0 ) {
4845+ var basicGate = this . basicGates [ gate . name ] ;
4846+ if ( basicGate ) {
4847+ if ( ops [ gate . name ] ) {
4848+ ops [ gate . name ] ++ ;
4849+ } else {
4850+ ops [ gate . name ] = 1 ;
4851+ }
4852+ } else {
4853+ var customGate = this . customGates [ gate . name ] ;
4854+ if ( customGate ) {
4855+ if ( ops [ gate . name ] ) {
4856+ ops [ gate . name ] ++ ;
4857+ } else {
4858+ ops [ gate . name ] = 1 ;
4859+ }
4860+
4861+ if ( ! options . shallow ) {
4862+ var subOps = this . countOps ( customGate , options ) ;
4863+ for ( subName in subOps ) {
4864+ if ( ops [ subName ] ) {
4865+ ops [ subName ] += subOps [ subName ] ;
4866+ } else {
4867+ ops [ subName ] = subOps [ subName ] ;
4868+ }
4869+ }
4870+ }
4871+ }
4872+ }
4873+ }
4874+ }
4875+ }
4876+
4877+ return ops ;
4878+ } ;
4879+
4880+
48254881//
48264882// Returns arrays of qubits where gates are present
48274883//
@@ -5234,7 +5290,6 @@ QuantumCircuit.prototype.load = function(obj) {
52345290 this . gates = JSON . parse ( JSON . stringify ( obj . gates || [ ] ) ) ;
52355291 this . customGates = JSON . parse ( JSON . stringify ( obj . customGates || { } ) ) ;
52365292 this . cregs = JSON . parse ( JSON . stringify ( obj . cregs || { } ) ) ;
5237-
52385293 // default options
52395294 this . options . params = this . options . params || { } ;
52405295 this . options . hybrid = this . options . hybrid || false ;
@@ -5584,6 +5639,19 @@ QuantumCircuit.prototype.getOrRegisterMultiControlledEquivalent = function(gateN
55845639} ;
55855640
55865641
5642+ QuantumCircuit . prototype . removeUnusedMultiControlledGates = function ( ) {
5643+ var ops = this . countOps ( null , { shallow : false } ) ;
5644+
5645+ for ( gateName in this . customGates ) {
5646+ if ( ! ops [ gateName ] ) {
5647+ if ( this . isMultiControlledGate ( gateName ) ) {
5648+ delete this . customGates [ gateName ] ;
5649+ }
5650+ }
5651+ }
5652+ } ;
5653+
5654+
55875655QuantumCircuit . prototype . decodeMultiControlledGateName = function ( gateName ) {
55885656 if ( ! gateName ) {
55895657 return null ;
0 commit comments