forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquantumcircuit.py
1347 lines (1121 loc) · 52.5 KB
/
quantumcircuit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Quantum circuit object."""
import copy
import itertools
import sys
import warnings
import multiprocessing as mp
from collections import OrderedDict
import numpy as np
from qiskit.util import is_main_process
from qiskit.circuit.instruction import Instruction
from qiskit.qasm.qasm import Qasm
from qiskit.circuit.exceptions import CircuitError
from .parameterexpression import ParameterExpression
from .quantumregister import QuantumRegister, Qubit
from .classicalregister import ClassicalRegister, Clbit
from .parametertable import ParameterTable
from .parametervector import ParameterVector
from .instructionset import InstructionSet
from .register import Register
from .bit import Bit
from .quantumcircuitdata import QuantumCircuitData
class QuantumCircuit:
"""Create a new circuit.
A circuit is a list of instructions bound to some registers.
Args:
regs: list(:class:`Register`) or list(``int``) The registers to be
included in the circuit.
* If a list of :class:`Register` objects, represents the :class:`QuantumRegister`
and/or :class:`ClassicalRegister` objects to include in the circuit.
For example:
* ``QuantumCircuit(QuantumRegister(4))``
* ``QuantumCircuit(QuantumRegister(4), ClassicalRegister(3))``
* ``QuantumCircuit(QuantumRegister(4, 'qr0'), QuantumRegister(2, 'qr1'))``
* If a list of ``int``, the amount of qubits and/or classical bits to include in
the circuit. It can either be a single int for just the number of quantum bits,
or 2 ints for the number of quantum bits and classical bits, respectively.
For example:
* ``QuantumCircuit(4) # A QuantumCircuit with 4 qubits``
* ``QuantumCircuit(4, 3) # A QuantumCircuit with 4 qubits and 3 classical bits``
name (str): the name of the quantum circuit. If not set, an
automatically generated string will be assigned.
Raises:
CircuitError: if the circuit name, if given, is not valid.
Examples:
Construct a simple Bell state circuit.
.. jupyter-execute::
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
qc.draw()
Construct a 5-qubit GHZ circuit.
.. jupyter-execute::
from qiskit import QuantumCircuit
qc = QuantumCircuit(5)
qc.h(0)
qc.cx(0, range(1, 5))
qc.measure_all()
Construct a 4-qubit Berstein-Vazirani circuit using registers.
.. jupyter-execute::
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
qr = QuantumRegister(3, 'q')
anc = QuantumRegister(1, 'ancilla')
cr = ClassicalRegister(3, 'c')
qc = QuantumCircuit(qr, anc, cr)
qc.x(anc[0])
qc.h(anc[0])
qc.h(qr[0:3])
qc.cx(qr[0:3], anc[0])
qc.h(qr[0:3])
qc.barrier(qr)
qc.measure(qr, cr)
qc.draw()
"""
instances = 0
prefix = 'circuit'
# Class variable OPENQASM header
header = "OPENQASM 2.0;"
extension_lib = "include \"qelib1.inc\";"
def __init__(self, *regs, name=None):
if any([not isinstance(reg, (QuantumRegister, ClassicalRegister)) for reg in regs]):
try:
regs = tuple(int(reg) for reg in regs)
except Exception:
raise CircuitError("Circuit args must be Registers or be castable to an int" +
"(%s '%s' was provided)"
% ([type(reg).__name__ for reg in regs], regs))
if name is None:
name = self.cls_prefix() + str(self.cls_instances())
if sys.platform != "win32" and not is_main_process():
name += '-{}'.format(mp.current_process().pid)
self._increment_instances()
if not isinstance(name, str):
raise CircuitError("The circuit name should be a string "
"(or None to auto-generate a name).")
self.name = name
# Data contains a list of instructions and their contexts,
# in the order they were applied.
self._data = []
# This is a map of registers bound to this circuit, by name.
self.qregs = []
self.cregs = []
self.add_register(*regs)
# Parameter table tracks instructions with variable parameters.
self._parameter_table = ParameterTable()
self._layout = None
@property
def data(self):
"""Return the circuit data (instructions and context).
Returns:
QuantumCircuitData: a list-like object containing the tuples for the circuit's data.
Each tuple is in the format ``(instruction, qargs, cargs)``, where instruction is an
Instruction (or subclass) object, qargs is a list of Qubit objects, and cargs is a
list of Clbit objects.
"""
return QuantumCircuitData(self)
@data.setter
def data(self, data_input):
"""Sets the circuit data from a list of instructions and context.
Args:
data_input (list): A list of instructions with context
in the format (instruction, qargs, cargs), where Instruction
is an Instruction (or subclass) object, qargs is a list of
Qubit objects, and cargs is a list of Clbit objects.
"""
# If data_input is QuantumCircuitData(self), clearing self._data
# below will also empty data_input, so make a shallow copy first.
data_input = data_input.copy()
self._data = []
for inst, qargs, cargs in data_input:
self.append(inst, qargs, cargs)
def __str__(self):
return str(self.draw(output='text'))
def __eq__(self, other):
# TODO: remove the DAG from this function
from qiskit.converters import circuit_to_dag
return circuit_to_dag(self) == circuit_to_dag(other)
@classmethod
def _increment_instances(cls):
cls.instances += 1
@classmethod
def cls_instances(cls):
"""Return the current number of instances of this class,
useful for auto naming."""
return cls.instances
@classmethod
def cls_prefix(cls):
"""Return the prefix to use for auto naming."""
return cls.prefix
def has_register(self, register):
"""
Test if this circuit has the register r.
Args:
register (Register): a quantum or classical register.
Returns:
bool: True if the register is contained in this circuit.
"""
has_reg = False
if (isinstance(register, QuantumRegister) and
register in self.qregs):
has_reg = True
elif (isinstance(register, ClassicalRegister) and
register in self.cregs):
has_reg = True
return has_reg
def mirror(self):
"""Mirror the circuit by reversing the instructions.
This is done by recursively mirroring all instructions.
It does not invert any gate.
Returns:
QuantumCircuit: the mirrored circuit
"""
reverse_circ = self.copy(name=self.name + '_mirror')
reverse_circ._data = []
for inst, qargs, cargs in reversed(self.data):
reverse_circ.append(inst.mirror(), qargs, cargs)
return reverse_circ
def inverse(self):
"""Invert this circuit.
This is done by recursively inverting all gates.
Returns:
QuantumCircuit: the inverted circuit
Raises:
CircuitError: if the circuit cannot be inverted.
"""
inverse_circ = self.copy(name=self.name + '_dg')
inverse_circ._data = []
for inst, qargs, cargs in reversed(self._data):
inverse_circ._data.append((inst.inverse(), qargs, cargs))
return inverse_circ
def combine(self, rhs):
"""Append rhs to self if self contains compatible registers.
Two circuits are compatible if they contain the same registers
or if they contain different registers with unique names. The
returned circuit will contain all unique registers between both
circuits.
Return self + rhs as a new object.
Args:
rhs (QuantumCircuit): The quantum circuit to append to the right hand side.
Returns:
QuantumCircuit: Returns a new QuantumCircuit object
Raises:
QiskitError: if the rhs circuit is not compatible
"""
# Check registers in LHS are compatible with RHS
self._check_compatible_regs(rhs)
# Make new circuit with combined registers
combined_qregs = copy.deepcopy(self.qregs)
combined_cregs = copy.deepcopy(self.cregs)
for element in rhs.qregs:
if element not in self.qregs:
combined_qregs.append(element)
for element in rhs.cregs:
if element not in self.cregs:
combined_cregs.append(element)
circuit = QuantumCircuit(*combined_qregs, *combined_cregs)
for instruction_context in itertools.chain(self.data, rhs.data):
circuit._append(*instruction_context)
return circuit
def extend(self, rhs):
"""Append QuantumCircuit to the right hand side if it contains compatible registers.
Two circuits are compatible if they contain the same registers
or if they contain different registers with unique names. The
returned circuit will contain all unique registers between both
circuits.
Modify and return self.
Args:
rhs (QuantumCircuit): The quantum circuit to append to the right hand side.
Returns:
QuantumCircuit: Returns this QuantumCircuit object (which has been modified)
Raises:
QiskitError: if the rhs circuit is not compatible
"""
# Check registers in LHS are compatible with RHS
self._check_compatible_regs(rhs)
# Add new registers
for element in rhs.qregs:
if element not in self.qregs:
self.qregs.append(element)
for element in rhs.cregs:
if element not in self.cregs:
self.cregs.append(element)
# Copy the circuit data if rhs and self are the same, otherwise the data of rhs is
# appended to both self and rhs resulting in an infinite loop
data = rhs.data.copy() if rhs is self else rhs.data
# Add new gates
for instruction_context in data:
self._append(*instruction_context)
return self
@property
def qubits(self):
"""
Returns a list of quantum bits in the order that the registers were added.
"""
return [qbit for qreg in self.qregs for qbit in qreg]
@property
def clbits(self):
"""
Returns a list of classical bits in the order that the registers were added.
"""
return [cbit for creg in self.cregs for cbit in creg]
def __add__(self, rhs):
"""Overload + to implement self.combine."""
return self.combine(rhs)
def __iadd__(self, rhs):
"""Overload += to implement self.extend."""
return self.extend(rhs)
def __len__(self):
"""Return number of operations in circuit."""
return len(self._data)
def __getitem__(self, item):
"""Return indexed operation."""
return self._data[item]
@staticmethod
def cast(value, _type):
"""Best effort to cast value to type. Otherwise, returns the value."""
try:
return _type(value)
except (ValueError, TypeError):
return value
@staticmethod
def _bit_argument_conversion(bit_representation, in_array):
ret = None
try:
if isinstance(bit_representation, Bit):
# circuit.h(qr[0]) -> circuit.h([qr[0]])
ret = [bit_representation]
elif isinstance(bit_representation, Register):
# circuit.h(qr) -> circuit.h([qr[0], qr[1]])
ret = bit_representation[:]
elif isinstance(QuantumCircuit.cast(bit_representation, int), int):
# circuit.h(0) -> circuit.h([qr[0]])
ret = [in_array[bit_representation]]
elif isinstance(bit_representation, slice):
# circuit.h(slice(0,2)) -> circuit.h([qr[0], qr[1]])
ret = in_array[bit_representation]
elif isinstance(bit_representation, list) and \
all(isinstance(bit, Bit) for bit in bit_representation):
# circuit.h([qr[0], qr[1]]) -> circuit.h([qr[0], qr[1]])
ret = bit_representation
elif isinstance(QuantumCircuit.cast(bit_representation, list), (range, list)):
# circuit.h([0, 1]) -> circuit.h([qr[0], qr[1]])
# circuit.h(range(0,2)) -> circuit.h([qr[0], qr[1]])
# circuit.h([qr[0],1]) -> circuit.h([qr[0], qr[1]])
ret = [index if isinstance(index, Bit) else in_array[
index] for index in bit_representation]
else:
raise CircuitError('Not able to expand a %s (%s)' % (bit_representation,
type(bit_representation)))
except IndexError:
raise CircuitError('Index out of range.')
except TypeError:
raise CircuitError('Type error handling %s (%s)' % (bit_representation,
type(bit_representation)))
return ret
def qbit_argument_conversion(self, qubit_representation):
"""
Converts several qubit representations (such as indexes, range, etc.)
into a list of qubits.
Args:
qubit_representation (Object): representation to expand
Returns:
List(tuple): Where each tuple is a qubit.
"""
return QuantumCircuit._bit_argument_conversion(qubit_representation, self.qubits)
def cbit_argument_conversion(self, clbit_representation):
"""
Converts several classical bit representations (such as indexes, range, etc.)
into a list of classical bits.
Args:
clbit_representation (Object): representation to expand
Returns:
List(tuple): Where each tuple is a classical bit.
"""
return QuantumCircuit._bit_argument_conversion(clbit_representation, self.clbits)
def append(self, instruction, qargs=None, cargs=None):
"""Append one or more instructions to the end of the circuit, modifying
the circuit in place. Expands qargs and cargs.
Args:
instruction (qiskit.circuit.Instruction): Instruction instance to append
qargs (list(argument)): qubits to attach instruction to
cargs (list(argument)): clbits to attach instruction to
Returns:
qiskit.circuit.Instruction: a handle to the instruction that was just added
"""
# Convert input to instruction
if not isinstance(instruction, Instruction) and hasattr(instruction, 'to_instruction'):
instruction = instruction.to_instruction()
expanded_qargs = [self.qbit_argument_conversion(qarg) for qarg in qargs or []]
expanded_cargs = [self.cbit_argument_conversion(carg) for carg in cargs or []]
instructions = InstructionSet()
for (qarg, carg) in instruction.broadcast_arguments(expanded_qargs, expanded_cargs):
instructions.add(self._append(instruction, qarg, carg), qarg, carg)
return instructions
def _append(self, instruction, qargs, cargs):
"""Append an instruction to the end of the circuit, modifying
the circuit in place.
Args:
instruction (Instruction or Operator): Instruction instance to append
qargs (list(tuple)): qubits to attach instruction to
cargs (list(tuple)): clbits to attach instruction to
Returns:
Instruction: a handle to the instruction that was just added
Raises:
CircuitError: if the gate is of a different shape than the wires
it is being attached to.
"""
if not isinstance(instruction, Instruction):
raise CircuitError('object is not an Instruction.')
# do some compatibility checks
self._check_dups(qargs)
self._check_qargs(qargs)
self._check_cargs(cargs)
# add the instruction onto the given wires
instruction_context = instruction, qargs, cargs
self._data.append(instruction_context)
self._update_parameter_table(instruction)
return instruction
def _update_parameter_table(self, instruction):
for param_index, param in enumerate(instruction.params):
if isinstance(param, ParameterExpression):
current_parameters = self.parameters
for parameter in param.parameters:
if parameter in current_parameters:
if not self._check_dup_param_spec(self._parameter_table[parameter],
instruction, param_index):
self._parameter_table[parameter].append((instruction, param_index))
else:
if parameter.name in {p.name for p in current_parameters}:
raise CircuitError(
'Name conflict on adding parameter: {}'.format(parameter.name))
self._parameter_table[parameter] = [(instruction, param_index)]
return instruction
def _check_dup_param_spec(self, parameter_spec_list, instruction, param_index):
for spec in parameter_spec_list:
if spec[0] is instruction and spec[1] == param_index:
return True
return False
def add_register(self, *regs):
"""Add registers."""
if not regs:
return
if any([isinstance(reg, int) for reg in regs]):
# QuantumCircuit defined without registers
if len(regs) == 1 and isinstance(regs[0], int):
# QuantumCircuit with anonymous quantum wires e.g. QuantumCircuit(2)
regs = (QuantumRegister(regs[0], 'q'),)
elif len(regs) == 2 and all([isinstance(reg, int) for reg in regs]):
# QuantumCircuit with anonymous wires e.g. QuantumCircuit(2, 3)
regs = (QuantumRegister(regs[0], 'q'), ClassicalRegister(regs[1], 'c'))
else:
raise CircuitError("QuantumCircuit parameters can be Registers or Integers."
" If Integers, up to 2 arguments. QuantumCircuit was called"
" with %s." % (regs,))
for register in regs:
if register.name in [reg.name for reg in self.qregs + self.cregs]:
raise CircuitError("register name \"%s\" already exists"
% register.name)
if isinstance(register, QuantumRegister):
self.qregs.append(register)
elif isinstance(register, ClassicalRegister):
self.cregs.append(register)
else:
raise CircuitError("expected a register")
def _check_dups(self, qubits):
"""Raise exception if list of qubits contains duplicates."""
squbits = set(qubits)
if len(squbits) != len(qubits):
raise CircuitError("duplicate qubit arguments")
def _check_qargs(self, qargs):
"""Raise exception if a qarg is not in this circuit or bad format."""
if not all(isinstance(i, Qubit) for i in qargs):
raise CircuitError("qarg is not a Qubit")
if not all(self.has_register(i.register) for i in qargs):
raise CircuitError("register not in this circuit")
def _check_cargs(self, cargs):
"""Raise exception if clbit is not in this circuit or bad format."""
if not all(isinstance(i, Clbit) for i in cargs):
raise CircuitError("carg is not a Clbit")
if not all(self.has_register(i.register) for i in cargs):
raise CircuitError("register not in this circuit")
def to_instruction(self, parameter_map=None):
"""Create an Instruction out of this circuit.
Args:
parameter_map(dict): For parameterized circuits, a mapping from
parameters in the circuit to parameters to be used in the
instruction. If None, existing circuit parameters will also
parameterize the instruction.
Returns:
qiskit.circuit.Instruction: a composite instruction encapsulating this circuit
(can be decomposed back)
"""
from qiskit.converters.circuit_to_instruction import circuit_to_instruction
return circuit_to_instruction(self, parameter_map)
def to_gate(self, parameter_map=None):
"""Create a Gate out of this circuit.
Args:
parameter_map(dict): For parameterized circuits, a mapping from
parameters in the circuit to parameters to be used in the
gate. If None, existing circuit parameters will also
parameterize the gate.
Returns:
Gate: a composite gate encapsulating this circuit
(can be decomposed back)
"""
from qiskit.converters.circuit_to_gate import circuit_to_gate
return circuit_to_gate(self, parameter_map)
def decompose(self):
"""Call a decomposition pass on this circuit,
to decompose one level (shallow decompose).
Returns:
QuantumCircuit: a circuit one level decomposed
"""
from qiskit.transpiler.passes.basis.decompose import Decompose
from qiskit.converters.circuit_to_dag import circuit_to_dag
from qiskit.converters.dag_to_circuit import dag_to_circuit
pass_ = Decompose()
decomposed_dag = pass_.run(circuit_to_dag(self))
return dag_to_circuit(decomposed_dag)
def _check_compatible_regs(self, rhs):
"""Raise exception if the circuits are defined on incompatible registers"""
list1 = self.qregs + self.cregs
list2 = rhs.qregs + rhs.cregs
for element1 in list1:
for element2 in list2:
if element2.name == element1.name:
if element1 != element2:
raise CircuitError("circuits are not compatible")
def qasm(self):
"""Return OpenQASM string."""
string_temp = self.header + "\n"
string_temp += self.extension_lib + "\n"
for register in self.qregs:
string_temp += register.qasm() + "\n"
for register in self.cregs:
string_temp += register.qasm() + "\n"
unitary_gates = []
for instruction, qargs, cargs in self._data:
if instruction.name == 'measure':
qubit = qargs[0]
clbit = cargs[0]
string_temp += "%s %s[%d] -> %s[%d];\n" % (instruction.qasm(),
qubit.register.name, qubit.index,
clbit.register.name, clbit.index)
else:
string_temp += "%s %s;\n" % (instruction.qasm(),
",".join(["%s[%d]" % (j.register.name, j.index)
for j in qargs + cargs]))
if instruction.name == 'unitary':
unitary_gates.append(instruction)
# this resets them, so if another call to qasm() is made the gate def is added again
for gate in unitary_gates:
gate._qasm_def_written = False
return string_temp
def draw(self, output=None, scale=0.7, filename=None, style=None,
interactive=False, line_length=None, plot_barriers=True,
reverse_bits=False, justify=None, vertical_compression='medium', idle_wires=True,
with_layout=True, fold=None, ax=None):
"""Draw the quantum circuit.
**text**: ASCII art TextDrawing that can be printed in the console.
**latex**: high-quality images compiled via LaTeX.
**latex_source**: raw uncompiled LaTeX output.
**matplotlib**: images with color rendered purely in Python.
Args:
output (str): Select the output method to use for drawing the
circuit. Valid choices are ``text``, ``latex``,
``latex_source``, or ``mpl``. By default the `'text`' drawer is
used unless a user config file has an alternative backend set
as the default. If the output kwarg is set, that backend
will always be used over the default in a user config file.
scale (float): scale of image to draw (shrink if < 1)
filename (str): file path to save image to
style (dict or str): dictionary of style or file name of style
file. This option is only used by the ``mpl`` output type. If a
str is passed in that is the path to a json file which contains
a dictionary of style, then that will be opened, parsed, and used
as the input dict. See: :ref:`Style Dict Doc <style-dict-circ-doc>` for more
information on the contents.
interactive (bool): when set true show the circuit in a new window
(for `mpl` this depends on the matplotlib backend being used
supporting this). Note when used with either the `text` or the
`latex_source` output type this has no effect and will be
silently ignored.
line_length (int): Deprecated; see `fold` which supersedes this
option. Sets the length of the lines generated by `text` output
type. This is useful when the drawing does not fit in the console.
If None (default), it will try to guess the console width using
``shutil.get_terminal_size()``. However, if you're running in
jupyter, the default line length is set to 80 characters. If you
don't want pagination at all, set ``line_length=-1``.
reverse_bits (bool): When set to True, reverse the bit order inside
registers for the output visualization.
plot_barriers (bool): Enable/disable drawing barriers in the output
circuit. Defaults to True.
justify (string): Options are ``left``, ``right`` or
``none``. If anything else is supplied it defaults to left
justified. It refers to where gates should be placed in the
output circuit if there is an option. ``none`` results in
each gate being placed in its own column.
vertical_compression (string): ``high``, ``medium`` or ``low``. It
merges the lines generated by the ``text`` output so the
drawing will take less vertical room. Default is ``medium``.
Only used by the ``text`` output, will be silently ignored
otherwise.
idle_wires (bool): Include idle wires (wires with no circuit
elements) in output visualization. Default is True.
with_layout (bool): Include layout information, with labels on the
physical layout. Default is True.
fold (int): Sets pagination. It can be disabled using -1.
In `text`, sets the length of the lines. This is useful when the
drawing does not fit in the console. If None (default), it will
try to guess the console width using ``shutil.
get_terminal_size()``. However, if running in jupyter, the
default line length is set to 80 characters. In ``mpl`` is the
number of (visual) layers before folding. Default is 25.
ax (matplotlib.axes.Axes): An optional Axes object to be used for
the visualization output. If none is specified, a new matplotlib
Figure will be created and used. Additionally, if specified,
there will be no returned Figure since it is redundant. This is
only used when the ``output`` kwarg is set to use the ``mpl``
backend. It will be silently ignored with all other outputs.
Returns:
:class:`PIL.Image` or :class:`matplotlib.figure` or :class:`str` or
:class:`TextDrawing`:
* `PIL.Image` (output='latex')
an in-memory representation of the image of the circuit
diagram.
* `matplotlib.figure.Figure` (output='mpl')
a matplotlib figure object for the circuit diagram.
* `str` (output='latex_source')
The LaTeX source code for visualizing the circuit diagram.
* `TextDrawing` (output='text')
A drawing that can be printed as ASCII art.
Raises:
VisualizationError: when an invalid output method is selected
ImportError: when the output methods require non-installed
libraries
.. _style-dict-circ-doc:
**Style Dict Details**
The style dict kwarg contains numerous options that define the style of
the output circuit visualization. The style dict is only used by the
``mpl`` output. The options available in the style dict are defined
below:
Args:
textcolor (str): The color code to use for text. Defaults to
`'#000000'`
subtextcolor (str): The color code to use for subtext. Defaults to
`'#000000'`
linecolor (str): The color code to use for lines. Defaults to
`'#000000'`
creglinecolor (str): The color code to use for classical register
lines. Defaults to `'#778899'`
gatetextcolor (str): The color code to use for gate text. Defaults
to `'#000000'`
gatefacecolor (str): The color code to use for gates. Defaults to
`'#ffffff'`
barrierfacecolor (str): The color code to use for barriers.
Defaults to `'#bdbdbd'`
backgroundcolor (str): The color code to use for the background.
Defaults to `'#ffffff'`
fontsize (int): The font size to use for text. Defaults to 13.
subfontsize (int): The font size to use for subtext. Defaults to 8.
displaytext (dict): A dictionary of the text to use for each
element type in the output visualization. The default values
are::
{
'id': 'id',
'u0': 'U_0',
'u1': 'U_1',
'u2': 'U_2',
'u3': 'U_3',
'x': 'X',
'y': 'Y',
'z': 'Z',
'h': 'H',
's': 'S',
'sdg': 'S^\\dagger',
't': 'T',
'tdg': 'T^\\dagger',
'rx': 'R_x',
'ry': 'R_y',
'rz': 'R_z',
'reset': '\\left|0\\right\\rangle'
}
You must specify all the necessary values if using this. There
is no provision for passing an incomplete dict in.
displaycolor (dict): The color codes to use for each circuit
element. The default values are::
{
'id': '#F0E442',
'u0': '#E7AB3B',
'u1': '#E7AB3B',
'u2': '#E7AB3B',
'u3': '#E7AB3B',
'x': '#58C698',
'y': '#58C698',
'z': '#58C698',
'h': '#70B7EB',
's': '#E0722D',
'sdg': '#E0722D',
't': '#E0722D',
'tdg': '#E0722D',
'rx': '#ffffff',
'ry': '#ffffff',
'rz': '#ffffff',
'reset': '#D188B4',
'target': '#70B7EB',
'meas': '#D188B4'
}
Also, just like `displaytext` there is no provision for an
incomplete dict passed in.
latexdrawerstyle (bool): When set to True, enable LaTeX mode, which
will draw gates like the `latex` output modes.
usepiformat (bool): When set to True, use radians for output.
fold (int): The number of circuit elements to fold the circuit at.
Defaults to 20.
cregbundle (bool): If set True, bundle classical registers
showindex (bool): If set True, draw an index.
compress (bool): If set True, draw a compressed circuit.
figwidth (int): The maximum width (in inches) for the output figure.
dpi (int): The DPI to use for the output image. Defaults to 150.
margin (list): A list of margin values to adjust spacing around
output image. Takes a list of 4 ints:
[x left, x right, y bottom, y top].
creglinestyle (str): The style of line to use for classical
registers. Choices are `'solid'`, `'doublet'`, or any valid
matplotlib `linestyle` kwarg value. Defaults to `doublet`
"""
# pylint: disable=cyclic-import
from qiskit.visualization import circuit_drawer
if isinstance(output, (int, float, np.number)):
warnings.warn("Setting 'scale' as the first argument is deprecated. "
"Use scale=%s instead." % output,
DeprecationWarning)
scale = output
output = None
return circuit_drawer(self, scale=scale,
filename=filename, style=style,
output=output,
interactive=interactive,
line_length=line_length,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits,
justify=justify,
vertical_compression=vertical_compression,
idle_wires=idle_wires,
with_layout=with_layout,
fold=fold,
ax=ax)
def size(self):
"""Returns total number of gate operations in circuit.
Returns:
int: Total number of gate operations.
"""
gate_ops = 0
for instr, _, _ in self._data:
if instr.name not in ['barrier', 'snapshot']:
gate_ops += 1
return gate_ops
def depth(self):
"""Return circuit depth (i.e., length of critical path).
This does not include compiler or simulator directives
such as 'barrier' or 'snapshot'.
Returns:
int: Depth of circuit.
Notes:
The circuit depth and the DAG depth need not be the
same.
"""
# Labels the registers by ints
# and then the qubit position in
# a register is given by reg_int+qubit_num
reg_offset = 0
reg_map = {}
for reg in self.qregs + self.cregs:
reg_map[reg.name] = reg_offset
reg_offset += reg.size
# If no registers return 0
if reg_offset == 0:
return 0
# A list that holds the height of each qubit
# and classical bit.
op_stack = [0] * reg_offset
# Here we are playing a modified version of
# Tetris where we stack gates, but multi-qubit
# gates, or measurements have a block for each
# qubit or cbit that are connected by a virtual
# line so that they all stacked at the same depth.
# Conditional gates act on all cbits in the register
# they are conditioned on.
# We treat barriers or snapshots different as
# They are transpiler and simulator directives.
# The max stack height is the circuit depth.
for instr, qargs, cargs in self._data:
levels = []
reg_ints = []
# If count then add one to stack heights
count = True
if instr.name in ['barrier', 'snapshot']:
count = False
for ind, reg in enumerate(qargs + cargs):
# Add to the stacks of the qubits and
# cbits used in the gate.
reg_ints.append(reg_map[reg.register.name] + reg.index)
if count:
levels.append(op_stack[reg_ints[ind]] + 1)
else:
levels.append(op_stack[reg_ints[ind]])
# Assuming here that there is no conditional
# snapshots or barriers ever.
if instr.condition:
# Controls operate over all bits in the
# classical register they use.
cint = reg_map[instr.condition[0].name]
for off in range(instr.condition[0].size):
if cint + off not in reg_ints:
reg_ints.append(cint + off)
levels.append(op_stack[cint + off] + 1)
max_level = max(levels)
for ind in reg_ints:
op_stack[ind] = max_level
return max(op_stack)
def width(self):
"""Return number of qubits plus clbits in circuit.
Returns:
int: Width of circuit.
"""
return sum(reg.size for reg in self.qregs + self.cregs)
@property
def n_qubits(self):
"""
Return number of qubits.
"""
qubits = 0
for reg in self.qregs:
qubits += reg.size
return qubits
@property
def n_clbits(self):
"""
Return number of classical bits.
"""
return sum(len(reg) for reg in self.cregs)
def count_ops(self):
"""Count each operation kind in the circuit.
Returns:
OrderedDict: a breakdown of how many operations of each kind, sorted by amount.
"""
count_ops = {}
for instr, _, _ in self._data:
if instr.name in count_ops.keys():
count_ops[instr.name] += 1
else:
count_ops[instr.name] = 1
return OrderedDict(sorted(count_ops.items(), key=lambda kv: kv[1], reverse=True))
def num_connected_components(self, unitary_only=False):
"""How many non-entangled subcircuits can the circuit be factored to.
Args:
unitary_only (bool): Compute only unitary part of graph.
Returns:
int: Number of connected components in circuit.