コード例 #1
0
def test_generate_unitary_gate():
    assert generate_unitary_gate("X") == XGate()
    assert generate_unitary_gate("Y") == YGate()
    assert generate_unitary_gate("S") == SGate()
    assert generate_unitary_gate("Z") == ZGate()
    assert generate_unitary_gate("H") == HGate()
    assert generate_unitary_gate("T") == TGate()
    assert generate_unitary_gate("I") == IdGate()
    for i in np.arange(-10, -1, 0.1):
        for j in np.arange(1, 10, 0.1):
            # Rx Gates
            assert generate_unitary_gate(f'Rx(pi*{j})') == RXGate(np.pi * j)
            assert generate_unitary_gate(f'Rx({i}/ {j}*pi)') == RXGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Rx( {i}*{j}/pi )') == RXGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Rx( {i} / {j}/pi)') == RXGate(
                i / j / np.pi)
            # Ry Gates
            assert generate_unitary_gate(f'Ry(pi*{j})') == RYGate(np.pi * j)
            assert generate_unitary_gate(f'Ry({i}/ {j}*pi)') == RYGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Ry( {i}*{j}/pi )') == RYGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Ry( {i} / {j}/pi)') == RYGate(
                i / j / np.pi)
            # Rz Gates
            assert generate_unitary_gate(f'Rz(pi*{j})') == RZGate(np.pi * j)
            assert generate_unitary_gate(f'Rz({i}/ {j}*pi)') == RZGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Rz( {i}*{j}/pi )') == RZGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Rz( {i} / {j}/pi)') == RZGate(
                i / j / np.pi)
コード例 #2
0
def generate_unitary_gate(gate_name: str) -> Gate:
    # Rx, Ry and Rz gates that look like 'Rx(pi/2)
    if gate_name[0] == 'R' and gate_name[2] == '(':
        angle = parse_angle(gate_name[3:-1])
        if gate_name[1] == 'x':
            return RXGate(angle)
        elif gate_name[1] == 'y':
            return RYGate(angle)
        elif gate_name[1] == 'z':
            return RZGate(angle)
    else:
        unitary_gates = {
            "X": XGate(),
            "Y": YGate(),
            "S": SGate(),
            "Z": ZGate(),
            "H": HGate(),
            "T": TGate(),
            "I": IGate(),
            "W": WGate(),
            "Rz1": RZGate(-3 * np.pi / 8),
            "Rz2": RZGate(np.pi / 2),
            "Ry1": RYGate(np.pi / 2)
        }
        return unitary_gates[gate_name]
コード例 #3
0
    def _gen_lookup_table(self):

        op1 = RZGate(-3 * np.pi / 8)
        op2 = RYGate(np.pi / 2)
        op3 = RZGate(np.pi / 2)
        result = {"X": XGate(), "Y": YGate(), "S": SGate(), "Z": ZGate(), "H": HGate(), "T": TGate(), "W": self._gen_w_gate(),
                  "Rz1": RZGate(-3 * np.pi / 8), "Rz2": RZGate(np.pi/2), "Ry1": RYGate(np.pi/2)}
        return result
コード例 #4
0
def crear_circuito(n, tipo):
    I_f = np.array([[1, 0], [0, 1]])
    I = np.array([[1, 0], [0, 1]])
    X_f = np.array([[0, 1], [1, 0]])
    X = np.array([[0, 1], [1, 0]])
    for q in range(n - 1):
        I_f = np.kron(I_f, I)
        X_f = np.kron(X_f, X)
    J = Operator(1 / np.sqrt(2) * (I_f + 1j * X_f))
    J_dg = J.adjoint()
    circ = QuantumCircuit(n, n)
    circ.append(J, range(n))
    if n == 1:
        dx = np.pi
        dy = 0
        dz = 0
    elif n == 2:
        # barrido
        dx = tipo[0]
        dy = tipo[1]
        dz = tipo[2]

    for q in range(n):
        circ.append(RXGate(dx), [q])
        circ.append(RYGate(dy), [q])
        circ.append(RZGate(dz), [q])

    circ.append(J_dg, range(n))
    circ.measure(range(n), range(n))
    return circ
def output_state(dx,dy,dz):
    I_f = np.array([[1, 0],
                [0, 1]]) 
    I = np.array([[1, 0],
                [0, 1]])
    X_f = np.array([[0, 1],
                [1, 0]]) 
    X = np.array([[0, 1],
                [1, 0]])    
    for q in range(1):
        I_f = np.kron(I_f, I)
        X_f = np.kron(X_f, X)
    J = Operator(1 / np.sqrt(2) * (I_f + 1j * X_f))    
    J_dg = J.adjoint()
    circ = QuantumCircuit(2,2)
    circ.append(J, range(2))

    for q in range(2):
        circ.append(RXGate(dx),[q])
        circ.append(RYGate(dy),[q])
        circ.append(RZGate(dz),[q])    

    circ.append(J_dg, range(2))
    backend = Aer.get_backend('statevector_simulator')
    job = backend.run(circ)
    result = job.result()
    outputstate = result.get_statevector(circ, decimals=5)
    return outputstate
コード例 #6
0
def create_circ(
    N,
    mu_,
    sig_,
):
    qr = QuantumRegister(N, 'q')
    qc = QuantumCircuit(
        qr
    )  # Generate a quantum circuit with a quantum register (list) of qubit objects
    alpha_0 = angle_(
        sig_, mu_)  # We multiply by 2, because the ry gate rotates by alpha/2
    qc.ry(
        2 * alpha_0, 0
    )  # apply a rotation angle of alpha_0 (multiply by 2 because gate halves parameter)
    for i in range(1, N):  # Steps to be done at level q_i
        qstring = ctrl_states(i)  # create list of 2^i strings of length i
        for k in qstring:
            alpha_ = angle_(sig_ / (2**i),
                            new_mu(k,
                                   mu_))  # Calculate angle using modified mean
            new_gate = RYGate(2 *
                              alpha_).control(num_ctrl_qubits=i,
                                              label=None,
                                              ctrl_state=k)  # control state is
            qc.append(new_gate, qr[:i + 1])  # add ry gate to level

    return qc
コード例 #7
0
    def testIfCorrectStrategyAndAccuracy(self):
        n_questions = 2
        tactic = [[1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]
        max_gates = 10
        env = Environment(n_questions, tactic, max_gates)
        save_state = env.initial_state.copy()
        nauceneVyhodil = [
            'b0r-78.75', 'b0r-78.75', 'a0r90.0', 'b0r-78.75', 'b1r56.25',
            'b1r-22.5', 'b0r11.25', 'b1r0.0', 'b1r0.0', 'b1r0.0'
        ]  # toto sa naucil
        dokopy = ['a0ry90', 'b0ry-225', 'b1ry33.75']
        for a in dokopy:
            env.step(a)

        A_0 = np.kron(RYGate((90 * pi / 180)).to_matrix(), np.identity(2))
        A_1 = np.kron(np.identity(2), np.identity(2))
        B_0 = np.kron(np.identity(2), RYGate((-225 * pi / 180)).to_matrix())
        B_1 = np.kron(np.identity(2), RYGate((33.75 * pi / 180)).to_matrix())

        ax = np.array([
            *[x for x in np.matmul(B_0, np.matmul(A_0, save_state))],
            *[x for x in np.matmul(B_1, np.matmul(A_0, save_state))],
            *[x for x in np.matmul(B_0, np.matmul(A_1, save_state))],
            *[x for x in np.matmul(B_1, np.matmul(A_1, save_state))]
        ])
        print(ax)
        print(env.accuracy)
        # assert (env.accuracy > 0.85) //TODO: este raz prekontrolovat ci je to spravne
        for poc, state in enumerate(env.repr_state):
            if poc % 4 == 0:
                assert (np.round(
                    env.repr_state[poc]**2 + env.repr_state[poc + 1]**2 +
                    env.repr_state[poc + 2]**2 + env.repr_state[poc + 3]**2,
                    2) == 1)

        for poc, state in enumerate(ax):
            if poc % 4 == 0:
                assert (np.round(
                    ax[poc]**2 + ax[poc + 1]**2 + ax[poc + 2]**2 +
                    ax[poc + 3]**2, 2) == 1)

        assert (env.repr_state.all() == ax.all())
コード例 #8
0
 def _define(self):
     """
     gate W a {
     Rz(-3*pi/8) a; Rz(pi/2) a; Ry(pi/2)
     }
     """
     definition = []
     q = QuantumRegister(1, "q")
     rule = [(RZGate(-3 * np.pi / 8), [q[0]], []),
             (RZGate(np.pi / 2), [q[0]], []),
             (RYGate(np.pi / 2), [q[0]], [])]
     for inst in rule:
         definition.append(inst)
     self.definition = definition
コード例 #9
0
    def test_case_06(self):
        circuit = QuantumCircuit(4)
        circuit.u3(5.3906, 5.3234, 3.918, 2)
        circuit.ccx(1, 3, 0)
        circuit.u1(4.9746, 1)
        circuit.cswap(0, 2, 3)
        circuit.cx(0, 1)
        circuit.append(RYGate(0.12704).control(1), [2, 3])
        circuit.measure_all()

        transpiled = transpile(circuit,
                               pass_manager=level_3_with_contant_pure(
                                   self.pm_conf))

        expected = self.execute(circuit).result()
        result = self.execute(transpiled).result()

        self.assertEqualCounts(result, expected)
コード例 #10
0
    def test_case_05(self):
        circuit = QuantumCircuit(3)
        circuit.z(0)
        circuit.h(1)
        circuit.u3(5.3906, 5.3234, 3.918, 2)
        circuit.t(1)
        circuit.swap(0, 2)
        circuit.cswap(2, 1, 0)
        circuit.append(RYGate(0.12704).control(1), [2, 0])
        circuit.ry(4.9042, 0)
        circuit.u3(6.014, 0.88185, 5.4669, 1)
        circuit.measure_all()

        transpiled = transpile(circuit,
                               pass_manager=level_3_with_contant_pure(
                                   self.pm_conf))

        expected = self.execute(circuit).result()
        result = self.execute(transpiled).result()

        self.assertEqualCounts(result, expected)
コード例 #11
0
 def testRYGate(self):
     assert (np.around(RYGate((0 * pi / 180)).to_matrix(),
                       5).all() == np.eye(2).all())
コード例 #12
0
            definition.append(inst)
        self.definition = definition


unitary_gates = {
    "X": XGate(),
    "Y": YGate(),
    "S": SGate(),
    "Z": ZGate(),
    "H": HGate(),
    "T": TGate(),
    "I": IdGate(),
    "W": WGate(),
    "Rz1": RZGate(-3 * np.pi / 8),
    "Rz2": RZGate(np.pi / 2),
    "Ry1": RYGate(np.pi / 2)
}


class Protocol(Enum):
    """
    The various different quantum/classical game theory game protocols
    """
    EWL = "EWL quantization protocol"
    MW = "MW quantization protocol"
    Classical = "Classical protocol"

    def describe(self):
        # self is the member here
        return self.name, self.value