Example #1
0
    def _define(self):
        definition = []
        # No controls:
        #   q = b
        # One Control:
        #   q = b, c1
        # Two Controls:
        #   q = c1, c2, b
        q = QuantumRegister(self.total_n, "q")
        rule = []

        angles = [0] * self.n
        for i in range(self.n):
            if self.a & (1 << i):
                for j in range(i, self.n):
                    angles[j] += np.pi / 2**(j - i)

        # Inverse of U1 gates is just a negative theta
        if self._inverse:
            for i in range(len(angles)):
                angles[i] *= -1

        # No controlled bits
        if self.c1 is None and self.c2 is None:
            for i in range(len(angles)):
                rule.append((U1Gate(angles[i]), [q[i]], []))

        # One controlled bit
        if self.c1 and self.c2 is None:
            for i in range(len(angles)):
                rule.append((Cu1Gate(angles[i]), [q[self.total_n - 1],
                                                  q[i]], []))

        # Two controlled bits
        # Uses sqrt of U1 gates which is just half of theta
        if self.c1 and self.c2:
            for i in range(len(angles)):
                rule.append((Cu1Gate(angles[i] / 2.), [q[1], q[i + 2]], []))
                # circ.cu1(angles[i]/2., c2, b[i])
            rule.append((CnotGate(), [q[0], q[1]], []))
            # circ.cx(c1, c2)

            for i in range(len(angles)):
                rule.append((Cu1Gate(-angles[i] / 2.), [q[1], q[i + 2]], []))
                # circ.cu1(-angles[i]/2., c2, b[i])
            rule.append((CnotGate(), [q[0], q[1]], []))
            # circ.cx(c1, c2)
            for i in range(len(angles)):
                rule.append((Cu1Gate(angles[i] / 2.), [q[0], q[i + 2]], []))
                # circ.cu1(angles[i]/2., c1, b[i])

        for inst in rule:
            definition.append(inst)
        self.definition = definition
Example #2
0
    def _define(self):
        definition = []
        q = QuantumRegister(self.n, "q")
        rule = []

        if not self._inverse:
            for j in range(self.n - 1, -1, -1):
                rule.append((HGate(), [q[j]], []))
                for i in range(j):
                    rule.append((Cu1Gate(np.pi / float(2**(i + 1))),
                                 [q[j - (i + 1)], q[j]], []))
        else:
            for j in range(self.n):
                for i in range(j - 1, -1, -1):
                    rule.append((Cu1Gate(-np.pi / float(2**(i + 1))),
                                 [q[j - (i + 1)], q[j]], []))
                rule.append((HGate(), [q[j]], []))

        for inst in rule:
            definition.append(inst)
        self.definition = definition
Example #3
0
 def test_controlled_u1(self):
     """Test creation of controlled u1 gate"""
     theta = 0.5
     self.assertEqual(U1Gate(theta).control(), Cu1Gate(theta))