Ejemplo n.º 1
0
    def __init__(self, coupling_map, target=None):
        """GateDirection pass.

        Args:
            coupling_map (CouplingMap): Directed graph represented a coupling map.
            target (Target): The backend target to use for this pass. If this is specified
                it will be used instead of the coupling map
        """
        super().__init__()
        self.coupling_map = coupling_map
        self.target = target

        # Create the replacement dag and associated register.
        self._cx_dag = DAGCircuit()
        qr = QuantumRegister(2)
        self._cx_dag.add_qreg(qr)
        self._cx_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[1]], [])
        self._cx_dag.apply_operation_back(CXGate(), [qr[1], qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[1]], [])

        self._ecr_dag = DAGCircuit()
        qr = QuantumRegister(2)
        self._ecr_dag.add_qreg(qr)
        self._ecr_dag.apply_operation_back(RYGate(-pi / 2), [qr[0]], [])
        self._ecr_dag.apply_operation_back(RYGate(pi / 2), [qr[1]], [])
        self._ecr_dag.apply_operation_back(ECRGate(), [qr[1], qr[0]], [])
        self._ecr_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._ecr_dag.apply_operation_back(HGate(), [qr[1]], [])
    def __init__(self, coupling_map):
        """GateDirection pass.

        Args:
            coupling_map (CouplingMap): Directed graph represented a coupling map.
        """
        super().__init__()
        self.coupling_map = coupling_map

        # Create the replacement dag and associated register.
        self._cx_dag = DAGCircuit()
        qr = QuantumRegister(2)
        self._cx_dag.add_qreg(qr)
        self._cx_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[1]], [])
        self._cx_dag.apply_operation_back(CXGate(), [qr[1], qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._cx_dag.apply_operation_back(HGate(), [qr[1]], [])

        self._ecr_dag = DAGCircuit()
        qr = QuantumRegister(2)
        self._ecr_dag.add_qreg(qr)
        self._ecr_dag.apply_operation_back(RYGate(-pi / 2), [qr[0]], [])
        self._ecr_dag.apply_operation_back(RYGate(pi / 2), [qr[1]], [])
        self._ecr_dag.apply_operation_back(ECRGate(), [qr[1], qr[0]], [])
        self._ecr_dag.apply_operation_back(HGate(), [qr[0]], [])
        self._ecr_dag.apply_operation_back(HGate(), [qr[1]], [])
Ejemplo n.º 3
0
 def specialize(self):
     self.a = self.b = np.pi / 4
     k2ltheta, k2lphi, k2llambda, k2lphase = _oneq_zyz.angles_and_phase(self.K2l)
     k2rtheta, k2rphi, k2rlambda, k2rphase = _oneq_zyz.angles_and_phase(self.K2r)
     self.global_phase += k2lphase + k2rphase
     self.K1r = self.K1r @ np.asarray(RZGate(k2lphi))
     self.K1l = self.K1l @ np.asarray(RZGate(k2rphi))
     self.K2l = np.asarray(RYGate(k2ltheta)) @ np.asarray(RZGate(k2llambda))
     self.K2r = np.asarray(RYGate(k2rtheta)) @ np.asarray(RZGate(k2rlambda))
Ejemplo n.º 4
0
 def _circuit_xyx(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL):
     gphase = phase - (phi + lam) / 2
     qr = QuantumRegister(1, "qr")
     circuit = QuantumCircuit(qr)
     if not simplify:
         atol = -1.0
     if abs(theta) < atol:
         tot = _mod_2pi(phi + lam, atol)
         if abs(tot) > atol:
             circuit._append(RXGate(tot), [qr[0]], [])
             gphase += tot / 2
         circuit.global_phase = gphase
         return circuit
     if abs(theta - np.pi) < atol:
         gphase += phi
         lam, phi = lam - phi, 0
     lam = _mod_2pi(lam, atol)
     if abs(lam) > atol:
         gphase += lam / 2
         circuit._append(RXGate(lam), [qr[0]], [])
     circuit._append(RYGate(theta), [qr[0]], [])
     phi = _mod_2pi(phi, atol)
     if abs(phi) > atol:
         gphase += phi / 2
         circuit._append(RXGate(phi), [qr[0]], [])
     circuit.global_phase = gphase
     return circuit
Ejemplo n.º 5
0
def crypto_hash(M, K, verbose=False):
    """
    M: length n bit-string message to encode
    K: list of d integers from 0 to N-1
    """
    n = len(M)  # num bits in message
    d = len(K)  # num keys
    N = 2**n

    num_qubits = int(log(d, 2)) + 1  #?????
    qr = QuantumRegister(num_qubits)
    qc = QuantumCircuit(qr)

    qc.h(range(num_qubits - 1))

    for j in range(len(M)):
        if M[j] == '1':
            for i in range(d):
                ctrl_state = bitstring(i, num_qubits - 1)

                theta = 4 * np.pi * int(K[i], 2) * 2**j / N

                Y_Rotate_Gate = RYGate(theta).control(num_qubits - 1,
                                                      ctrl_state=ctrl_state)
                qc.compose(Y_Rotate_Gate, qr, inplace=True)

    if verbose:
        print(qc.draw())

    backend = Aer.get_backend('statevector_simulator')
    state = execute(qc, backend).result().get_statevector()
    return (state)
Ejemplo n.º 6
0
 def specialize(self):
     self.b = self.c = (self.b + self.c) / 2
     k2ltheta, k2lphi, k2llambda, k2lphase = _oneq_xyx.angles_and_phase(self.K2l)
     self.global_phase += k2lphase
     self.K1r = self.K1r @ np.asarray(RXGate(k2lphi))
     self.K1l = self.K1l @ np.asarray(RXGate(k2lphi))
     self.K2l = np.asarray(RYGate(k2ltheta)) @ np.asarray(RXGate(k2llambda))
     self.K2r = np.asarray(RXGate(-k2lphi)) @ self.K2r
Ejemplo n.º 7
0
 def _circuit_xyx(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL):
     circuit = QuantumCircuit(1, global_phase=phase)
     if simplify and np.isclose(theta, 0.0, atol=atol):
         circuit.append(RXGate(phi + lam), [0])
         return circuit
     if not simplify or not np.isclose(lam, 0.0, atol=atol):
         circuit.append(RXGate(lam), [0])
     if not simplify or not np.isclose(theta, 0.0, atol=atol):
         circuit.append(RYGate(theta), [0])
     if not simplify or not np.isclose(phi, 0.0, atol=atol):
         circuit.append(RXGate(phi), [0])
     return circuit
Ejemplo n.º 8
0
 def _circuit_zyz(theta, phi, lam, simplify=True, atol=DEFAULT_ATOL):
     circuit = QuantumCircuit(1)
     if simplify and np.isclose(theta, 0.0, atol=atol):
         circuit.append(RZGate(phi + lam), [0])
         return circuit
     if not simplify or not np.isclose(lam, 0.0, atol=atol):
         circuit.append(RZGate(lam), [0])
     if not simplify or not np.isclose(theta, 0.0, atol=atol):
         circuit.append(RYGate(theta), [0])
     if not simplify or not np.isclose(phi, 0.0, atol=atol):
         circuit.append(RZGate(phi), [0])
     return circuit
Ejemplo n.º 9
0
    def Cn(qc, entry, n, q, controls, targ):

        for i in range(n):
            if controls[i] == 0:
                qc.x(q[i])

        qc.append(RYGate(2 * np.arccos(entry)).control(n), q + [targ], [])

        for i in range(n):
            if controls[i] == 0:
                qc.x(q[i])

        return (qc)
Ejemplo n.º 10
0
def crypto_hash_final(qc_hash, qr, d, bitstring, n, big_N, K):
    for n_index in range(1, n + 1):
        control_states = "0" * (d - 1)
        if int(bitstring[n_index - 1]) == 1:
            for perm in range(0, 2**(d - 1)):
                control_states = dec_to_bin(perm, d)
                i = sum(int(c) for c in control_states.strip())
                Y_Rotate_Gate = RYGate(np.pi * K[i - 1] * 2**n_index /
                                       big_N).control(
                                           d - 1, ctrl_state=control_states)
                qc_hash.compose(Y_Rotate_Gate, qr, inplace=True)

    return qc_hash
 def _circuit_xyx(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL):
     qr = QuantumRegister(1, 'qr')
     circuit = QuantumCircuit(qr, global_phase=phase)
     if simplify and math.isclose(theta, 0.0, abs_tol=atol):
         circuit._append(RXGate(phi + lam), [qr[0]], [])
         return circuit
     if not simplify or not math.isclose(lam, 0.0, abs_tol=atol):
         circuit._append(RXGate(lam), [qr[0]], [])
     if not simplify or not math.isclose(theta, 0.0, abs_tol=atol):
         circuit._append(RYGate(theta), [qr[0]], [])
     if not simplify or not math.isclose(phi, 0.0, abs_tol=atol):
         circuit._append(RXGate(phi), [qr[0]], [])
     return circuit
Ejemplo n.º 12
0
def encodeDist(dist, regBounds):
    """
    Discretize the distribution dist into multiple regions with boundaries
    given by regBounds, and store the associated quantum superposition
    state in a new quantum register reg. Please make sure the number of 
    regions is a power of 2, i.e. len(regBounds) = (2 ** n) + 1.

    Additionally, the number of regions is limited to a maximum of
    2^(n // 2 + 1), where n is the number of qubits available in the backend
    being used - this is due to the requirement of (n - 2) ancilla qubits in
    order to perform (n - 1) control operations with minimal possible depth.

    Returns a new quantum circuit containing the instructions and registers
    needed to create the superposition state, along with the size of the
    quantum register.
    """
    numQubits = int(log2(len(regBounds) - 1))

    a = QuantumRegister(2 * numQubits - 2)
    c = ClassicalRegister(numQubits)
    qc = QuantumCircuit(a, c)

    for i in range(numQubits):
        numRegions = int(2 ** (i + 1))

        for j in range(numRegions // 2):
            prob = computeRegionProbability(dist, regBounds, numRegions, j)

            if not i:
                qc.ry(2 * arccos(sqrt(prob)), a[2 * numQubits - 3])
            else:
                cGate = RYGate(2 * arccos(sqrt(prob))).control(i)
                listOfFlips = getFlipList(i, j, numQubits)

                for k in listOfFlips:
                    qc.x(a[k])

                qubitsUsed = [a[k] for k in
                    range(2 * numQubits - 2 - i, 2 * numQubits - 2)]

                qubitsUsed.append(a[2 * numQubits - 3 - i])
                qc.append(cGate, qubitsUsed)

                for k in listOfFlips:
                    qc.x(a[k])

    return qc, a, c
Ejemplo n.º 13
0
key = pair[1]

print("Key")
print(key)
print("")
print("Angles: ")
angles = gen_angles(data)

nqubits = int(math.log2(len(data_points)))
nclassical = nqubits
q = QuantumRegister(nqubits)
c = ClassicalRegister(nclassical)
circ = QuantumCircuit(q, c)

a = Parameter('a')
CCRY = RYGate(a).control(2, ctrl_state = '10')

print("Levels:")

for k in range(len(angles)):  # now works
    j = 0
    if (k > 0):
        j = int(math.log2(k + 1)) # level in the binary tree 
    
    print(j)
    
    if (j == 0):
        circ.ry(angles[k], nqubits - 1 - j)
        
    
    """for i in range(j):