def duplicate_instruction(inst): """Create a fresh instruction from an input instruction.""" if issubclass(inst.__class__, Instruction) and inst.__class__ not in [ Instruction, Gate]: if inst.name == 'barrier': new_inst = inst.__class__(inst.num_qubits) elif inst.name == 'initialize': params = getattr(inst, 'params', []) new_inst = inst.__class__(params) elif inst.name == 'snapshot': label = inst.params[0] snap_type = inst.params[1] new_inst = inst.__class__(inst.num_qubits, inst.num_clbits, label, snap_type) else: params = getattr(inst, 'params', []) new_inst = inst.__class__(*params) else: if isinstance(inst, Gate): new_inst = Gate(inst.name, inst.num_qubits, inst.params) else: new_inst = Instruction(name=inst.name, num_qubits=inst.num_qubits, num_clbits=inst.num_clbits, params=inst.params) new_inst.definition = inst.definition return new_inst
def _create_op(self, name, params): if name in self.standard_extension: op = self.standard_extension[name](*params) elif name in self.gates: op = Gate(name=name, num_qubits=self.gates[name]['n_bits'], params=params) if not self.gates[name]['opaque']: # call a custom gate (otherwise, opaque) op.definition = self._gate_rules_to_qiskit_circuit(self.gates[name], params=params) else: raise QiskitError("unknown operation for ast node name %s" % name) return op
def inverse(self): """Return the inverse. Note that the resulting Gate object has an empty ``params`` property. """ inverse_gate = Gate( name=self.name + "_dg", num_qubits=self.num_qubits, params=[]) # removing the params because arrays are deprecated definition = QuantumCircuit(*self.definition.qregs) for inst in reversed(self._definition): definition._append( inst.replace(operation=inst.operation.inverse())) inverse_gate.definition = definition return inverse_gate
def inverse(self): """Return the inverse. Note that the resulting Gate object has an empty ``params`` property. """ inverse_gate = Gate( name=self.name + "_dg", num_qubits=self.num_qubits, params=[] ) # removing the params because arrays are deprecated inverse_gate.definition = QuantumCircuit(*self.definition.qregs) inverse_gate.definition._data = [ (inst.inverse(), qargs, []) for inst, qargs, _ in reversed(self._definition) ] return inverse_gate
def _create_op(self, name, params): if name in self.standard_extension: op = self.standard_extension[name](*params) elif name in self.gates: if self.gates[name]['opaque']: # call an opaque gate op = Gate(name=name, num_qubits=self.gates[name]['n_bits'], params=params) else: # call a custom gate op = Instruction(name=name, num_qubits=self.gates[name]['n_bits'], num_clbits=0, params=params) op.definition = self._gate_definition_to_qiskit_definition(self.gates[name], params=params) else: raise QiskitError("unknown operation for ast node name %s" % name) return op