@@ -5471,6 +5471,7 @@ QuantumCircuit.prototype.registerGate = function(name, obj) {
5471
5471
connectors . push ( rootGate . drawingInfo . connectors [ rootGate . drawingInfo . connectors . length - 1 ] ) ;
5472
5472
5473
5473
this . customGates [ name ] . drawingInfo . connectors = connectors ;
5474
+ this . customGates [ name ] . drawingInfo . root = rootGate . drawingInfo . root ;
5474
5475
}
5475
5476
}
5476
5477
} ;
@@ -5479,12 +5480,80 @@ QuantumCircuit.prototype.registerGate = function(name, obj) {
5479
5480
QuantumCircuit . prototype . registerMCXGate = function ( ctrlQubits ) {
5480
5481
var gateName = this . multiControlledGateName ( "mcx" , ctrlQubits ) ;
5481
5482
this . registerGate ( gateName , this . MCXCircuit ( ctrlQubits ) . save ( true ) ) ;
5483
+ return gateName ;
5482
5484
} ;
5483
5485
5484
5486
5485
5487
QuantumCircuit . prototype . registerMCU1Gate = function ( ctrlQubits ) {
5486
5488
var gateName = this . multiControlledGateName ( "mcu1" , ctrlQubits ) ;
5487
5489
this . registerGate ( gateName , this . MCU1Circuit ( ctrlQubits ) . save ( true ) ) ;
5490
+ return gateName ;
5491
+ } ;
5492
+
5493
+
5494
+ QuantumCircuit . prototype . registerMultiControlledGate = function ( rootName , ctrlQubits ) {
5495
+ switch ( rootName ) {
5496
+ case "cx" : return this . registerMCXGate ( ctrlQubits ) ; break ;
5497
+ case "cu1" : return this . registerMCU1Gate ( ctrlQubits ) ; break ;
5498
+ }
5499
+
5500
+ return "" ;
5501
+ } ;
5502
+
5503
+ QuantumCircuit . prototype . getOrRegisterMultiControlledEquivalent = function ( gateName , inverseControl ) {
5504
+ var gateDef = this . basicGates [ gateName ] ;
5505
+ if ( gateDef ) {
5506
+ if ( gateDef . drawingInfo ) {
5507
+ // Basic gate with multi-controlled/inverse controlled implementation
5508
+ if ( gateName == "x" || gateName == "u1" || gateDef . drawingInfo . root == "x" || gateDef . drawingInfo . root == "u1" ) {
5509
+ if ( ! this . basicGates [ "c" + gateName ] || inverseControl ) {
5510
+ // create multi controlled version and return its name
5511
+ var rootName = "c" + ( gateDef . drawingInfo . root || gateName ) ;
5512
+ var numCtrlQubits = math . log2 ( gateDef . matrix . length ) ;
5513
+ var ctrlQubits = [ ] ;
5514
+ for ( var i = 0 ; i < numCtrlQubits - 1 ; i ++ ) {
5515
+ ctrlQubits . push ( true ) ;
5516
+ }
5517
+ ctrlQubits . unshift ( ! inverseControl ) ;
5518
+
5519
+ return this . registerMultiControlledGate ( rootName , ctrlQubits ) ;
5520
+ } else {
5521
+ // there is basic gate with additional control
5522
+ return "c" + gateName ;
5523
+ }
5524
+ }
5525
+ }
5526
+
5527
+ // Basic gate for which we don't have multi-controlled or inverse controlled implementation
5528
+ // but maybe we have basic gate with additional control
5529
+ for ( var gn in this . basicGates ) {
5530
+ var tmpGateDef = this . basicGates [ gn ] ;
5531
+ if ( tmpGateDef && tmpGateDef . drawingInfo && tmpGateDef . drawingInfo . root && tmpGateDef . drawingInfo . root == gateName ) {
5532
+ if ( ! inverseControl ) {
5533
+ // there is basic gate with additional control
5534
+ return gn ;
5535
+ }
5536
+ }
5537
+ }
5538
+ return null ;
5539
+ }
5540
+
5541
+ if ( this . customGates [ gateName ] ) {
5542
+ var mcInfo = this . decodeMultiControlledGateName ( gateName ) ;
5543
+ if ( ! mcInfo || ! mcInfo . numCtrlQubits ) {
5544
+ return null ;
5545
+ }
5546
+
5547
+ if ( mcInfo . rootName == "cx" || mcInfo . rootName == "cu1" ) {
5548
+ // create multi controlled version and return its name
5549
+ mcInfo . ctrlQubits . unshift ( ! inverseControl ) ;
5550
+ return this . registerMultiControlledGate ( mcInfo . rootName , mcInfo . ctrlQubits ) ;
5551
+ }
5552
+
5553
+ return null ;
5554
+ }
5555
+
5556
+ return null ;
5488
5557
} ;
5489
5558
5490
5559
@@ -5561,17 +5630,21 @@ QuantumCircuit.prototype.multiControlledGateName = function(namePrefix, ctrlQubi
5561
5630
return gateName ;
5562
5631
} ;
5563
5632
5633
+
5564
5634
QuantumCircuit . prototype . isMultiControlledGate = function ( gateName ) {
5565
5635
var mcInfo = this . decodeMultiControlledGateName ( gateName ) ;
5566
5636
return ! ! this . customGates [ gateName ] && ! ! mcInfo && ! ! mcInfo . numCtrlQubits ;
5567
5637
} ;
5568
5638
5569
- QuantumCircuit . prototype . isControllableGate = function ( gateName ) {
5570
- if ( gateName == "cx" ) {
5571
- return true ;
5572
- }
5573
5639
5574
- if ( this . basicGates [ gateName ] ) {
5640
+ QuantumCircuit . prototype . isControllableGate = function ( gateName ) {
5641
+ var gateDef = this . basicGates [ gateName ] ;
5642
+ if ( gateDef ) {
5643
+ if ( gateDef . drawingInfo ) {
5644
+ if ( gateDef . drawingInfo . root == "x" || gateDef . drawingInfo . root == "u1" ) {
5645
+ return true ;
5646
+ }
5647
+ }
5575
5648
for ( var gn in this . basicGates ) {
5576
5649
var gateDef = this . basicGates [ gn ] ;
5577
5650
if ( gateDef && gateDef . drawingInfo && gateDef . drawingInfo . root && gateDef . drawingInfo . root == gateName ) {
@@ -5580,8 +5653,7 @@ QuantumCircuit.prototype.isControllableGate = function(gateName) {
5580
5653
}
5581
5654
}
5582
5655
5583
- // return this.isMultiControlledGate(gateName);
5584
- return false ;
5656
+ return this . isMultiControlledGate ( gateName ) ;
5585
5657
} ;
5586
5658
5587
5659
QuantumCircuit . prototype . getGatePosById = function ( gateId ) {
@@ -7428,14 +7500,13 @@ QuantumCircuit.prototype.exportToSVG = function(options) {
7428
7500
gateList . push ( { name : "dot" , svg : dotsvg } ) ;
7429
7501
}
7430
7502
7431
- /*
7432
7503
// special item: inverted dot
7433
7504
var ndotsvg = "" ;
7434
7505
if ( ! options . embedded ) {
7435
7506
ndotsvg += "<?xml version=\"1.0\"?>" ;
7436
7507
ndotsvg += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" ;
7437
7508
}
7438
- ndotsvg += "<svg class=\"qc-gate-gallery-item\" data-gate=\"dot \" data-content=\"Control\" width=\"" + options.cellWidth + "\" height=\"" + options.cellHeight + "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">";
7509
+ ndotsvg += "<svg class=\"qc-gate-gallery-item\" data-gate=\"ndot \" data-content=\"Control\" width=\"" + options . cellWidth + "\" height=\"" + options . cellHeight + "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" ;
7439
7510
ndotsvg = ndotsvg + gateSVG ( 0 , 0 , "ndot" , "ndot" , "ndot" , ! ! options . gateGallery , false ) ;
7440
7511
ndotsvg += "</svg>" ;
7441
7512
@@ -7444,10 +7515,8 @@ QuantumCircuit.prototype.exportToSVG = function(options) {
7444
7515
} else {
7445
7516
gateList . push ( { name : "dot" , svg : ndotsvg } ) ;
7446
7517
}
7447
- */
7448
7518
}
7449
7519
7450
-
7451
7520
// custom gates
7452
7521
if ( options . customGateGallery ) {
7453
7522
for ( var gateName in this . customGates ) {
0 commit comments