@@ -402,10 +402,10 @@ def to_text_diagram(
402402 """Returns text containing a diagram describing the circuit.
403403
404404 Args:
405- ext: For extending gates to implement AsciiDiagrammableGate .
406- use_unicode_characters: Activates the use of cleaner-looking
407- unicode box-drawing characters for lines .
408- transpose: Arranges the wires vertically instead of horizontally.
405+ ext: For extending gates to implement TextDiagrammableGate .
406+ use_unicode_characters: Determines if unicode characters are
407+ allowed (as opposed to ascii-only diagrams) .
408+ transpose: Arranges qubit wires vertically instead of horizontally.
409409 qubit_order_key: Transforms each qubit into a key that determines
410410 how the qubits are ordered in the diagram. Qubits with lower
411411 keys come first. Defaults to the qubit's __str__, but augmented
@@ -414,7 +414,43 @@ def to_text_diagram(
414414 "name2").
415415
416416 Returns:
417- The ascii diagram.
417+ The text diagram.
418+ """
419+
420+ diagram = self .to_text_diagram_drawer (
421+ ext = ext ,
422+ qubit_name_suffix = '' if transpose else ': ' ,
423+ qubit_order_key = qubit_order_key )
424+
425+ if transpose :
426+ return diagram .transpose ().render (
427+ crossing_char = '─' if use_unicode_characters else '-' ,
428+ use_unicode_characters = use_unicode_characters )
429+ return diagram .render (
430+ crossing_char = '┼' if use_unicode_characters else '|' ,
431+ horizontal_spacing = 3 ,
432+ use_unicode_characters = use_unicode_characters )
433+
434+ def to_text_diagram_drawer (
435+ self ,
436+ ext : Extensions = Extensions (),
437+ qubit_name_suffix : str = '' ,
438+ qubit_order_key : Callable [[QubitId ], Any ] = None
439+ ) -> TextDiagramDrawer :
440+ """Returns a TextDiagramDrawer with the circuit drawn into it.
441+
442+ Args:
443+ ext: For extending gates to implement TextDiagrammableGate.
444+ qubit_name_suffix: Appended to qubit names in the diagram.
445+ qubit_order_key: Transforms each qubit into a key that determines
446+ how the qubits are ordered in the diagram. Qubits with lower
447+ keys come first. Defaults to the qubit's __str__, but augmented
448+ so that lexicographic ordering will respect the order of
449+ integers within the string (e.g. "name10" will come after
450+ "name2").
451+
452+ Returns:
453+ The TextDiagramDrawer instance.
418454 """
419455 if qubit_order_key is None :
420456 qubit_order_key = _str_lexicographic_respecting_int_order
@@ -432,7 +468,7 @@ def to_text_diagram(
432468
433469 diagram = TextDiagramDrawer ()
434470 for q , i in qubit_map .items ():
435- diagram .write (0 , i , str (q ) + ( '' if transpose else ': ' ) )
471+ diagram .write (0 , i , str (q ) + qubit_name_suffix )
436472
437473 for moment in [Moment ()] * 2 + self .moments + [Moment ()]:
438474 _draw_moment_in_diagram (moment , ext , qubit_map , diagram )
@@ -441,28 +477,21 @@ def to_text_diagram(
441477 for i in qubit_map .values ():
442478 diagram .horizontal_line (i , 0 , w )
443479
444- if transpose :
445- return diagram .transpose ().render (
446- crossing_char = '─' if use_unicode_characters else '-' ,
447- use_unicode_characters = use_unicode_characters )
448- return diagram .render (
449- crossing_char = '┼' if use_unicode_characters else '|' ,
450- horizontal_spacing = 3 ,
451- use_unicode_characters = use_unicode_characters )
480+ return diagram
452481
453482
454483def _get_operation_text_diagram_symbols (op : ops .Operation , ext : Extensions
455484 ) -> Iterable [str ]:
456- ascii_gate = ext .try_cast (op .gate , ops .AsciiDiagrammableGate )
457- if ascii_gate is not None :
458- wire_symbols = ascii_gate . ascii_wire_symbols ()
485+ text_diagram_gate = ext .try_cast (op .gate , ops .TextDiagrammableGate )
486+ if text_diagram_gate is not None :
487+ wire_symbols = text_diagram_gate . text_diagram_wire_symbols ()
459488 if len (op .qubits ) == len (wire_symbols ):
460489 return wire_symbols
461490 elif len (wire_symbols ) == 1 :
462491 return len (op .qubits ) * wire_symbols
463492 else :
464493 raise ValueError (
465- 'Multi-qubit operation with AsciiDiagrammableGate {} that '
494+ 'Multi-qubit operation with TextDiagrammableGate {} that '
466495 'requires {} qubits but found {} qubits' .format (
467496 repr (op .gate ), len (wire_symbols ), len (op .qubits )))
468497
@@ -474,10 +503,10 @@ def _get_operation_text_diagram_symbols(op: ops.Operation, ext: Extensions
474503
475504def _get_operation_text_diagram_exponent (op : ops .Operation ,
476505 ext : Extensions ) -> Optional [str ]:
477- ascii_gate = ext .try_cast (op .gate , ops .AsciiDiagrammableGate )
478- if ascii_gate is None :
506+ text_diagram_gate = ext .try_cast (op .gate , ops .TextDiagrammableGate )
507+ if text_diagram_gate is None :
479508 return None
480- exponent = ascii_gate . ascii_exponent ()
509+ exponent = text_diagram_gate . text_diagram_exponent ()
481510 if exponent == 1 :
482511 return None
483512 if isinstance (exponent , float ):
0 commit comments