def get_param_str(op, drawer, ndigits=3): """Get the params as a string to add to the gate text display""" if not hasattr(op, "params") or any( isinstance(param, np.ndarray) for param in op.params): return "" if isinstance(op, Delay): param_list = [f"{op.params[0]}[{op.unit}]"] else: param_list = [] for count, param in enumerate(op.params): # Latex drawer will cause an xy-pic error and mpl drawer will overwrite # the right edge if param string too long, so limit params. if (drawer == "latex" and count > 3) or (drawer == "mpl" and count > 15): param_list.append("...") break try: param_list.append( pi_check(param, output=drawer, ndigits=ndigits)) except TypeError: param_list.append(str(param)) param_str = "" if param_list: if drawer == "latex": param_str = f"\\,(\\mathrm{{{','.join(param_list)}}})" elif drawer == "mpl": param_str = f"{', '.join(param_list)}".replace("-", "$-$") else: param_str = f"({','.join(param_list)})" return param_str
def build_gate_call(self, instruction: CircuitInstruction): """Builds a QuantumGateCall""" if isinstance(instruction.operation, standard_gates.UGate): gate_name = ast.Identifier("U") else: gate_name = ast.Identifier( self.global_namespace[instruction.operation]) qubits = [ self.build_single_bit_reference(qubit) for qubit in instruction.qubits ] if self.disable_constants: parameters = [ ast.Expression(self._rebind_scoped_parameters(param)) for param in instruction.operation.params ] else: parameters = [ ast.Expression( pi_check(self._rebind_scoped_parameters(param), output="qasm")) for param in instruction.operation.params ] return ast.QuantumGateCall(gate_name, qubits, parameters=parameters)
def build_quantumgatecall(self, instruction): """Builds a QuantumGateCall""" if isinstance(instruction[0], UGate): quantumGateName = Identifier("U") else: quantumGateName = Identifier(self.global_namespace[instruction[0]]) indexIdentifierList = self.build_indexIdentifierlist(instruction[1]) if self.disable_constants: parameters = [Expression(param) for param in instruction[0].params] else: parameters = [ Expression(pi_check(param, output="qasm")) for param in instruction[0].params ] return QuantumGateCall(quantumGateName, indexIdentifierList, parameters=parameters)
def __repr__(self): # FIXME: this is worth making prettier since it's very useful for debugging return ("{}\n{}\n{}\nUd({}, {}, {})\n{}\n{}\n".format( pi_check(self.global_phase), np.array_str(self.K1l), np.array_str(self.K1r), self.a, self.b, self.c, np.array_str(self.K2l), np.array_str(self.K2r)))