def _check_creg(self, register):
     """Raise exception if r is not in this circuit or not creg."""
     if not isinstance(register, ClassicalRegister):
         raise QISKitError("expected classical register")
     if not self.has_register(register):
         raise QISKitError("register '%s' not in this circuit" %
                           register.name)
 def _check_qreg(self, register):
     """Raise exception if r is not in this circuit or not qreg."""
     if not isinstance(register, QuantumRegister):
         raise QISKitError("expected quantum register")
     if not self.has_register(register):
         raise QISKitError("register '%s' not in this circuit" %
                           register.name)
Exemple #3
0
 def add(self, *regs):
     """Add registers."""
     for register in regs:
         if register.name in self.qregs or register.name in self.cregs:
             raise QISKitError("register name \"%s\" already exists" %
                               register.name)
         if isinstance(register, QuantumRegister):
             self.qregs[register.name] = register
         elif isinstance(register, ClassicalRegister):
             self.cregs[register.name] = register
         else:
             raise QISKitError("expected a register")
    def __init__(self, *regs, name=None):
        """Create a new circuit.

        A circuit is a list of instructions bound to some registers.

        Args:
            *regs (Registers): registers to include in the circuit.
            name (str or None): the name of the quantum circuit. If
                None, an automatically generated string will be assigned.

        Raises:
            QISKitError: if the circuit name, if given, is not valid.
        """
        if name is None:
            name = self.cls_prefix() + str(self.cls_instances())
        self._increment_instances()

        if not isinstance(name, str):
            raise QISKitError("The circuit name should be a string "
                              "(or None to auto-generate a name).")

        self.name = name

        # Data contains a list of instructions in the order they were applied.
        self.data = []

        # This is a map of registers bound to this circuit, by name.
        self.qregs = OrderedDict()
        self.cregs = OrderedDict()
        self.add(*regs)

        self.annealergraph = annealer_graph(self.qregs)
 def _check_qubit(self, qubit):
     """Raise exception if qubit is not in this circuit or bad format."""
     if not isinstance(qubit, tuple):
         raise QISKitError("%s is not a tuple."
                           "A qubit should be formated as a tuple." %
                           str(qubit))
     if not len(qubit) == 2:
         raise QISKitError(
             "%s is not a tuple with two elements, but %i instead" %
             len(qubit))
     if not isinstance(qubit[1], int):
         raise QISKitError(
             "The second element of a tuple defining a qubit should be an int:"
             "%s was found instead" % type(qubit[1]).__name__)
     self._check_qreg(qubit[0])
     qubit[0].check_range(qubit[1])
 def _check_compatible_regs(self, rhs):
     """Raise exception if the circuits are defined on incompatible registers"""
     lhs_regs = {**self.qregs, **self.cregs}
     rhs_regs = {**rhs.qregs, **rhs.cregs}
     common_registers = lhs_regs.keys() & rhs_regs.keys()
     for name in common_registers:
         if lhs_regs[name] != rhs_regs[name]:
             raise QISKitError("circuits are not compatible")
    def add(self, *regs):
        """Add registers."""
        if 'annealergraph' in dir(self):
            for r in regs:
                for i in range(r.size):
                    if '{}_{}'.format(r.name,
                                      i) not in self.annealergraph.qubits:
                        self.annealergraph.qubits['{}_{}'.format(
                            r.name, i)] = {
                                'components': list(),
                                'measured': False
                            }

        for register in regs:
            if register.name in self.qregs or register.name in self.cregs:
                raise QISKitError("register name \"%s\" already exists" %
                                  register.name)
            if isinstance(register, QuantumRegister):
                self.qregs[register.name] = register
            elif isinstance(register, ClassicalRegister):
                self.cregs[register.name] = register
            else:
                raise QISKitError("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 QISKitError("duplicate qubit arguments")