Example #1
0
 def _define_decompositions(self):
     """
     gate cu3(theta,phi,lambda) c, t
     { u1((lambda-phi)/2) t; cx c,t;
       u3(-theta/2,0,-(phi+lambda)/2) t; cx c,t;
       u3(theta/2,phi,0) t;
     }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(2, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("u1", 1, 0, 1)
     decomposition.add_basis_element("u3", 1, 0, 3)
     decomposition.add_basis_element("cx", 2, 0, 0)
     rule = [
         U1Gate((self.params[2] - self.params[1]) / 2, q[1]),
         CnotGate(q[0], q[1]),
         U3Gate(-self.params[0] / 2, 0,
                -(self.params[1] + self.params[2]) / 2, q[1]),
         CnotGate(q[0], q[1]),
         U3Gate(self.params[0] / 2, self.params[1], 0, q[1])
     ]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #2
0
 def _define_decompositions(self):
     decomposition = DAGCircuit()
     q = QuantumRegister(1, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("U", 1, 0, 3)
     rule = [UBase(self.params[0], self.params[1], self.params[2], q[0])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #3
0
 def _define_decompositions(self):
     decomposition = DAGCircuit()
     q = QuantumRegister(1, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("u1", 1, 0, 1)
     rule = [U1Gate(pi, q[0])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #4
0
 def _define_decompositions(self):
     """
     gate ry(theta) a { u3(theta, 0, 0) a; }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(1, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("u3", 1, 0, 3)
     rule = [U3Gate(self.params[0], 0, 0, q[0])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #5
0
 def _define_decompositions(self):
     """
     gate h a { u2(0,pi) a; }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(1, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("u2", 1, 0, 2)
     rule = [U2Gate(0, pi, q[0])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #6
0
 def _define_decompositions(self):
     """
     gate cx c,t { CX c,t; }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(2, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("CX", 2, 0, 0)
     rule = [CXBase(q[0], q[1])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #7
0
 def _define_decompositions(self):
     """
     gate cz a,b { h b; cx a,b; h b; }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(2, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("h", 1, 0, 0)
     decomposition.add_basis_element("cx", 2, 0, 0)
     rule = [HGate(q[1]), CnotGate(q[0], q[1]), HGate(q[1])]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #8
0
def dag_to_circuit(dag):
    """Build a ``QuantumCircuit`` object from a ``DAGCircuit``.

    Args:
        dag (DAGCircuit): the input dag.

    Return:
        QuantumCircuit: the circuit representing the input dag.
    """
    qregs = collections.OrderedDict()
    for qreg in dag.qregs.values():
        qreg_tmp = QuantumRegister(qreg.size, name=qreg.name)
        qregs[qreg.name] = qreg_tmp
    cregs = collections.OrderedDict()
    for creg in dag.cregs.values():
        creg_tmp = ClassicalRegister(creg.size, name=creg.name)
        cregs[creg.name] = creg_tmp

    name = dag.name or None
    circuit = QuantumCircuit(*qregs.values(), *cregs.values(), name=name)

    graph = dag.multi_graph
    for node in nx.topological_sort(graph):
        n = graph.nodes[node]
        if n['type'] == 'op':
            if n['op'].name == 'U':
                name = 'u_base'
            elif n['op'].name == 'CX':
                name = 'cx_base'
            elif n['op'].name == 'id':
                name = 'iden'
            else:
                name = n['op'].name

            instr_method = getattr(circuit, name)
            qubits = []
            for qubit in n['qargs']:
                qubits.append(qregs[qubit[0].name][qubit[1]])

            clbits = []
            for clbit in n['cargs']:
                clbits.append(cregs[clbit[0].name][clbit[1]])
            params = n['op'].params

            if name in ['snapshot', 'save', 'noise', 'load']:
                result = instr_method(params[0])
            else:
                result = instr_method(*params, *qubits, *clbits)
            if 'condition' in n and n['condition']:
                result.c_if(*n['condition'])
    return circuit
Example #9
0
 def _define_decompositions(self):
     """
     gate rzz(theta) a, b { cx a, b; u1(theta) b; cx a, b; }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(2, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("u1", 1, 0, 1)
     decomposition.add_basis_element("cx", 2, 0, 0)
     rule = [
         CnotGate(q[0], q[1]),
         U1Gate(self.params[0], q[0]),
         CnotGate(q[0], q[1])
     ]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #10
0
 def _define_decompositions(self):
     """
     gate cswap a,b,c
     { cx c,b;
       ccx a,b,c;
       cx c,b;
     }
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(3, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("cx", 2, 0, 0)
     decomposition.add_basis_element("ccx", 3, 0, 0)
     rule = [
         CnotGate(q[2], q[1]),
         ToffoliGate(q[0], q[1], q[2]),
         CnotGate(q[2], q[1])
     ]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #11
0
 def _define_decompositions(self):
     """
     gate ccx a,b,c
     {
     h c; cx b,c; tdg c; cx a,c;
     t c; cx b,c; tdg c; cx a,c;
     t b; t c; h c; cx a,b;
     t a; tdg b; cx a,b;}
     """
     decomposition = DAGCircuit()
     q = QuantumRegister(3, "q")
     decomposition.add_qreg(q)
     decomposition.add_basis_element("h", 1, 0, 0)
     decomposition.add_basis_element("cx", 2, 0, 0)
     decomposition.add_basis_element("t", 1, 0, 0)
     decomposition.add_basis_element("tdg", 1, 0, 0)
     decomposition.add_basis_element("s", 1, 0, 0)
     decomposition.add_basis_element("sdg", 1, 0, 0)
     rule = [
         HGate(q[2]),
         CnotGate(q[1], q[2]),
         TdgGate(q[2]),
         CnotGate(q[0], q[2]),
         TGate(q[2]),
         CnotGate(q[1], q[2]),
         TdgGate(q[2]),
         CnotGate(q[0], q[2]),
         TGate(q[1]),
         TGate(q[2]),
         HGate(q[2]),
         CnotGate(q[0], q[1]),
         TGate(q[0]),
         TdgGate(q[1]),
         CnotGate(q[0], q[1])
     ]
     for inst in rule:
         decomposition.apply_operation_back(inst)
     self._decompositions = [decomposition]
Example #12
0
    def _process_node(self, node):
        """Carry out the action associated with a node."""
        if node.type == "program":
            self._process_children(node)

        elif node.type == "qreg":
            qreg = QuantumRegister(node.index, node.name)
            self.dag.add_qreg(qreg)

        elif node.type == "creg":
            creg = ClassicalRegister(node.index, node.name)
            self.dag.add_creg(creg)

        elif node.type == "id":
            raise ReNomQError("internal error: _process_node on id")

        elif node.type == "int":
            raise ReNomQError("internal error: _process_node on int")

        elif node.type == "real":
            raise ReNomQError("internal error: _process_node on real")

        elif node.type == "indexed_id":
            raise ReNomQError("internal error: _process_node on indexed_id")

        elif node.type == "id_list":
            # We process id_list nodes when they are leaves of barriers.
            return [
                self._process_bit_id(node_children)
                for node_children in node.children
            ]

        elif node.type == "primary_list":
            # We should only be called for a barrier.
            return [self._process_bit_id(m) for m in node.children]

        elif node.type == "gate":
            self._process_gate(node)

        elif node.type == "custom_unitary":
            self._process_custom_unitary(node)

        elif node.type == "universal_unitary":
            args = self._process_node(node.children[0])
            qid = self._process_bit_id(node.children[1])
            for element in qid:
                self.dag.apply_operation_back(UBase(*args, element),
                                              self.condition)

        elif node.type == "cnot":
            self._process_cnot(node)

        elif node.type == "expression_list":
            return node.children

        elif node.type == "binop":
            raise ReNomQError("internal error: _process_node on binop")

        elif node.type == "prefix":
            raise ReNomQError("internal error: _process_node on prefix")

        elif node.type == "measure":
            self._process_measure(node)

        elif node.type == "format":
            self.version = node.version()

        elif node.type == "barrier":
            ids = self._process_node(node.children[0])
            qubits = []
            for qubit in ids:
                for j, _ in enumerate(qubit):
                    qubits.append(qubit[j])
            self.dag.apply_operation_back(Barrier(qubits))

        elif node.type == "reset":
            id0 = self._process_bit_id(node.children[0])
            for i, _ in enumerate(id0):
                self.dag.apply_operation_back(Reset(id0[i]), self.condition)

        elif node.type == "if":
            self._process_if(node)

        elif node.type == "opaque":
            self._process_gate(node, opaque=True)

        elif node.type == "external":
            raise ReNomQError("internal error: _process_node on external")

        else:
            raise ReNomQError("internal error: undefined node type", node.type,
                              "line=%s" % node.line, "file=%s" % node.file)
        return None