def _circuit_diagram_info_(self, args: protocols.CircuitDiagramInfoArgs ) -> Union[str, protocols.CircuitDiagramInfo]: if self._global_shift == -0.5: return _rads_func_symbol( 'Rz', args, self._diagram_exponent(args, ignore_global_phase=False)) e = self._diagram_exponent(args) if e in [-0.25, 0.25]: return protocols.CircuitDiagramInfo( wire_symbols=('T',), exponent=cast(float, e) * 4) if e in [-0.5, 0.5]: return protocols.CircuitDiagramInfo( wire_symbols=('S',), exponent=cast(float, e) * 2) return protocols.CircuitDiagramInfo( wire_symbols=('Z',), exponent=e)
def _circuit_diagram_info_(self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: """See `cirq.SupportsCircuitDiagramInfo`.""" if (isinstance(self.phase_exponent, sympy.Symbol) or args.precision is None): s = 'PhasedX({})'.format(self.phase_exponent) else: s = 'PhasedX({{:.{}}})'.format(args.precision).format( self.phase_exponent) return protocols.CircuitDiagramInfo( wire_symbols=(s,), exponent=value.canonicalize_half_turns(self._exponent))
def qcircuit_diagram_info(self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: info = protocols.circuit_diagram_info(self.sub, args) multigate_parameters = _get_multigate_parameters(self.sub, args) if multigate_parameters is not None: min_index, n_qubits = multigate_parameters name = _escape_text_for_latex(str(self.sub).rsplit('**', 1)[0]) if info.exponent != 1: name += '^{' + str(info.exponent) + '}' box = '\multigate{' + str(n_qubits - 1) + '}{' + name + '}' ghost = '\ghost{' + name + '}' assert args.qubit_map is not None assert args.known_qubits is not None symbols = tuple(box if (args.qubit_map[q] == min_index) else ghost for q in args.known_qubits) return protocols.CircuitDiagramInfo(symbols, exponent=info.exponent, connected=False) s = [_escape_text_for_latex(e) for e in info.wire_symbols] if info.exponent != 1: s[0] += '^{' + str(info.exponent) + '}' return protocols.CircuitDiagramInfo(tuple('\\gate{' + e + '}' for e in s))
def _pauli_string_diagram_info( self, args: protocols.CircuitDiagramInfoArgs, exponent: Any = 1, exponent_absorbs_sign: bool = False, ) -> protocols.CircuitDiagramInfo: qubits = self.qubits if args.known_qubits is None else args.known_qubits syms = tuple('[{}]'.format(self.pauli_string[qubit]) for qubit in qubits) if exponent_absorbs_sign and self.pauli_string.coefficient == -1: # TODO: generalize to other coefficients. exponent = -exponent return protocols.CircuitDiagramInfo(wire_symbols=syms, exponent=exponent)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': diagram_info = protocols.circuit_diagram_info(self.gate, args, NotImplemented) if diagram_info == NotImplemented: return diagram_info # Include symbols for every qubit instead of just one symbol = diagram_info.wire_symbols[0] wire_symbols = (symbol,) * len(self.qubits) return protocols.CircuitDiagramInfo( wire_symbols=wire_symbols, exponent=diagram_info.exponent, connected=False )
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': symbols = [f'M({g})' for g in self._observable] # Mention the measurement key. label_map = args.label_map or {} if not args.known_qubits or self.key != _default_measurement_key(args.known_qubits): if self.key not in label_map: symbols[0] += f"('{self.key}')" if self.key in label_map: symbols += '@' return protocols.CircuitDiagramInfo(tuple(symbols))
def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': labels = cast(Dict[pauli_gates.Pauli, np.ndarray], { pauli_gates.X: 'X', pauli_gates.Y: 'Y', pauli_gates.Z: '@' }) l0 = labels[self.pauli0] l1 = labels[self.pauli1] # Add brackets around letter if inverted l0, l1 = (f'(-{l})' if inv else l for l, inv in ((l0, self.invert0), (l1, self.invert1))) return protocols.CircuitDiagramInfo( wire_symbols=(l0, l1), exponent=self._diagram_exponent(args))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': labels: Dict['cirq.Pauli', str] = { pauli_gates.X: 'X', pauli_gates.Y: 'Y', pauli_gates.Z: '@', } l0 = labels[self.pauli0] l1 = labels[self.pauli1] # Add brackets around letter if inverted l0 = f'(-{l0})' if self.invert0 else l0 l1 = f'(-{l1})' if self.invert1 else l1 return protocols.CircuitDiagramInfo( wire_symbols=(l0, l1), exponent=self._diagram_exponent(args))
def hardcoded_qcircuit_diagram_info(op: ops.Operation) -> Optional[protocols.CircuitDiagramInfo]: if not isinstance(op, ops.GateOperation): return None symbols = ( (r'\targ',) if op.gate == ops.X else (r'\control', r'\control') if op.gate == ops.CZ else (r'\control', r'\targ') if op.gate == ops.CNOT else (r'\meter',) if isinstance(op.gate, ops.MeasurementGate) else () ) return protocols.CircuitDiagramInfo(cast(Tuple[str, ...], symbols)) if symbols else None
def _pauli_string_diagram_info(self, args: protocols.CircuitDiagramInfoArgs, exponent: Any = 1, exponent_absorbs_sign: bool = False, ) -> protocols.CircuitDiagramInfo: qubits = self.qubits if args.known_qubits is None else args.known_qubits syms = tuple('[{}]'.format(self.pauli_string[qubit]) for qubit in qubits) if exponent_absorbs_sign and self.pauli_string.negated: if isinstance(exponent, float): exponent = -exponent else: exponent = '-{!s}'.format(exponent) return protocols.CircuitDiagramInfo(wire_symbols=syms, exponent=exponent)
def _circuit_diagram_info_(self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: symbols = ['M'] * self.num_qubits() # Show which output bits are negated. if self.invert_mask: for i, b in enumerate(self.invert_mask): if b: symbols[i] = '!M' # Mention the measurement key. if (not args.known_qubits or self.key != _default_measurement_key( args.known_qubits)): symbols[0] += "('{}')".format(self.key) return protocols.CircuitDiagramInfo(tuple(symbols))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': symbols = ['M'] * self.num_qubits() # Show which output bits are negated. if self.invert_mask: for i, b in enumerate(self.invert_mask): if b: symbols[i] = '!M' # Mention the measurement key if it is non-trivial or there are no known qubits. if self.mkey.name or self.mkey.path or not args.known_qubits: symbols[0] += f"('{self.key}')" return protocols.CircuitDiagramInfo(tuple(symbols))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': rounded_angles = np.array(self._diag_angles_radians) if args.precision is not None: rounded_angles = rounded_angles.round(args.precision) if len(rounded_angles) <= 4: rounded_angles_str = ', '.join(proper_repr(angle) for angle in rounded_angles) diag_str = f'diag({rounded_angles_str})' else: diag_str = ', '.join(proper_repr(angle) for angle in rounded_angles[:2]) diag_str += ', ..., ' diag_str += ', '.join(proper_repr(angle) for angle in rounded_angles[-2:]) diag_str = f'diag({diag_str})' return protocols.CircuitDiagramInfo( [diag_str] + ['#' + str(i) for i in range(2, self._num_qubits_() + 1)] )
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: sub_args = protocols.CircuitDiagramInfoArgs( known_qubit_count=(args.known_qubit_count - self.num_controls() if args.known_qubit_count is not None else None), known_qubits=(args.known_qubits[self.num_controls():] if args.known_qubits is not None else None), use_unicode_characters=args.use_unicode_characters, precision=args.precision, qubit_map=args.qubit_map) sub_info = protocols.circuit_diagram_info(self.sub_gate, sub_args, None) if sub_info is None: return NotImplemented return protocols.CircuitDiagramInfo( wire_symbols=('@', ) * self.num_controls() + sub_info.wire_symbols, exponent=sub_info.exponent)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': coefficient = '' if self._observable.coefficient == 1 else '-' symbols = [ f'M({"" if i else coefficient}{self._observable[i]})' for i in range(len(self._observable)) ] # Mention the measurement key. label_map = args.label_map or {} if not args.known_qubits or self.key != _default_measurement_key( args.known_qubits): if self.key not in label_map: symbols[0] += f"('{self.key}')" if self.key in label_map: symbols += '@' return protocols.CircuitDiagramInfo(tuple(symbols))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': symbols = ['M'] * self.num_qubits() # Show which output bits are negated. if self.invert_mask: for i, b in enumerate(self.invert_mask): if b: symbols[i] = '!M' # Mention the measurement key. label_map = args.label_map or {} if not args.known_qubits or self.key != _default_measurement_key(args.known_qubits): if self.key not in label_map: symbols[0] += f"('{self.key}')" if self.key in label_map: symbols += '@' return protocols.CircuitDiagramInfo(symbols)
def _circuit_diagram_info_(self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: e = 0 if args.precision is None else 10**-args.precision half_turns = self.exponent if isinstance(self.phase_exponent, value.Symbol): s = 'W({})'.format(self.phase_exponent) elif abs(self.phase_exponent) <= e: s = 'X' elif (abs(self.phase_exponent - 1) <= e and isinstance(half_turns, float)): s = 'X' half_turns = -half_turns elif abs(self.phase_exponent - 0.5) <= e: s = 'Y' elif args.precision is not None: s = 'W({{:.{}}})'.format(args.precision).format( self.phase_exponent) else: s = 'W({})'.format(self.phase_exponent) return protocols.CircuitDiagramInfo((s,), half_turns)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> Optional['protocols.CircuitDiagramInfo']: n = len(self.controls) sub_args = protocols.CircuitDiagramInfoArgs( known_qubit_count=(args.known_qubit_count - n if args.known_qubit_count is not None else None), known_qubits=(args.known_qubits[n:] if args.known_qubits is not None else None), use_unicode_characters=args.use_unicode_characters, precision=args.precision, label_map=args.label_map, ) sub_info = protocols.circuit_diagram_info(self.sub_operation, sub_args, None) if sub_info is None: return NotImplemented def get_symbol(vals): if tuple(vals) == (1, ): return '@' return f"({','.join(map(str, vals))})" wire_symbols = (*(get_symbol(vals) for vals in self.control_values), *sub_info.wire_symbols) exponent_qubit_index = None if sub_info.exponent_qubit_index is not None: exponent_qubit_index = sub_info.exponent_qubit_index + len( self.control_values) elif sub_info.exponent is not None: # For a multi-qubit `sub_operation`, if the `exponent_qubit_index` is None, the qubit # on which the exponent gets drawn in the controlled case (smallest ordered qubit of # sub_operation) can be different from the uncontrolled case (lexicographically largest # qubit of sub_operation). See tests for example. exponent_qubit_index = len(self.control_values) return protocols.CircuitDiagramInfo( wire_symbols=wire_symbols, exponent=sub_info.exponent, exponent_qubit_index=exponent_qubit_index, )
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> Optional[protocols.CircuitDiagramInfo]: n = len(self.controls) sub_args = protocols.CircuitDiagramInfoArgs( known_qubit_count=(args.known_qubit_count - n if args.known_qubit_count is not None else None), known_qubits=(args.known_qubits[n:] if args.known_qubits is not None else None), use_unicode_characters=args.use_unicode_characters, precision=args.precision, qubit_map=args.qubit_map) sub_info = protocols.circuit_diagram_info(self.sub_operation, sub_args, None) if sub_info is None: return NotImplemented return protocols.CircuitDiagramInfo(wire_symbols=('@', ) * n + sub_info.wire_symbols, exponent=sub_info.exponent)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': if not args.use_unicode_characters: return protocols.CircuitDiagramInfo(('@', 'swap', 'swap')) return protocols.CircuitDiagramInfo(('@', '×', '×'))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': return protocols.CircuitDiagramInfo( ('@', '@', 'X'), exponent=self._diagram_exponent(args), exponent_qubit_index=2 )
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: return protocols.CircuitDiagramInfo( wire_symbols=('@', 'X'), exponent=self._diagram_exponent(args))
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: return protocols.CircuitDiagramInfo(wire_symbols=('I', ) * self.num_qubits(), connected=True)
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: return protocols.CircuitDiagramInfo( wire_symbols=(_matrix_to_diagram_symbol(self._matrix, args), ))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': return protocols.CircuitDiagramInfo( wire_symbols=('iSwap', 'iSwap'), exponent=self._diagram_exponent(args))
def _circuit_diagram_info_( self, args: protocols.CircuitDiagramInfoArgs ) -> protocols.CircuitDiagramInfo: wire_symbols = tuple('' if i is None else str(i) for i in self.indices) return protocols.CircuitDiagramInfo(wire_symbols, connected=False)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': s = f'PhISwap({args.format_real(self._phase_exponent)})' return protocols.CircuitDiagramInfo( wire_symbols=(s, s), exponent=self._diagram_exponent(args))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> 'cirq.CircuitDiagramInfo': """Circuit symbol for qids defaults to the string representation.""" return protocols.CircuitDiagramInfo(wire_symbols=(str(self),))
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo': wire_symbols = tuple(f'({i})' for i in self.logical_indices) return protocols.CircuitDiagramInfo(wire_symbols=wire_symbols)
def _circuit_diagram_info_( self, args: 'cirq.CircuitDiagramInfoArgs' ) -> Union[str, 'protocols.CircuitDiagramInfo']: return protocols.CircuitDiagramInfo( wire_symbols=(f'GPI({self.phase!r})', ))