示例#1
0
    def create_input_gate(self, x, uin_type):
        # Encode x into quantum state
        # x = 2-dim. variables, [-1,1]
        u = QuantumCircuit(self.nqubit)

        angle_y = np.arcsin(x)
        angle_z = np.arccos(x**2)

        if uin_type == 0:
            for i in range(self.nqubit):
                u.add_RY_gate(i, angle_y[i])
                u.add_RZ_gate(i, angle_z[i])

        elif uin_type == 1:
            #for d in range(2):
            for i in range(self.nqubit):
                u.add_H_gate(i)
                u.add_RY_gate(i, angle_y[i])
                u.add_RZ_gate(i, angle_z[i])
            # KT: add second order expansion
            for i in range(self.nqubit - 1):
                for j in range(i + 1, self.nqubit):
                    angle_z2 = np.arccos(x[i] * x[j])
                    u.add_CNOT_gate(i, j)
                    u.add_RZ_gate(j, angle_z2)
                    u.add_CNOT_gate(i, j)

        return u
示例#2
0
def U_in(x):
    U = QuantumCircuit(nqubit)

    angle_y = np.arcsin(x)
    angle_z = np.arccos(x**2)

    for i in range(nqubit):
        U.add_RY_gate(i, angle_y)
        U.add_RZ_gate(i, angle_z)

    return U
def main():
    import numpy as np
    n_qubit = 2
    obs = Observable(n_qubit)
    initial_state = QuantumState(n_qubit)
    obs.add_operator(1, "Z 0 Z 1")
    circuit_list = []
    p_list = [0.02, 0.04, 0.06, 0.08]

    #prepare circuit list
    for p in p_list:
        circuit = QuantumCircuit(n_qubit)
        circuit.add_H_gate(0)
        circuit.add_RY_gate(1, np.pi / 6)
        circuit.add_CNOT_gate(0, 1)
        circuit.add_gate(
            Probabilistic([p / 4, p / 4, p / 4],
                          [X(0), Y(0), Z(0)]))  #depolarizing noise
        circuit.add_gate(
            Probabilistic([p / 4, p / 4, p / 4],
                          [X(1), Y(1), Z(1)]))  #depolarizing noise
        circuit_list.append(circuit)

    #get mitigated output
    mitigated, non_mitigated_array, fit_coefs = error_mitigation_extrapolate_linear(
        circuit_list,
        p_list,
        initial_state,
        obs,
        n_circuit_sample=100000,
        return_full=True)

    #plot the result
    p = np.linspace(0, max(p_list), 100)
    plt.plot(p,
             fit_coefs[0] * p + fit_coefs[1],
             linestyle="--",
             label="linear fit")
    plt.scatter(p_list, non_mitigated_array, label="un-mitigated")
    plt.scatter(0, mitigated, label="mitigated output")

    #prepare the clean result
    state = QuantumState(n_qubit)
    circuit = QuantumCircuit(n_qubit)
    circuit.add_H_gate(0)
    circuit.add_RY_gate(1, np.pi / 6)
    circuit.add_CNOT_gate(0, 1)
    circuit.update_quantum_state(state)
    plt.scatter(0, obs.get_expectation_value(state), label="True output")
    plt.xlabel("error rate")
    plt.ylabel("expectation value")
    plt.legend()
    plt.show()
    def create_input_gate(self, x):
        # 単一のxをエンコードするゲートを作成する関数
        # xは入力特徴量(2次元)
        # xの要素は[-1, 1]の範囲内
        u = QuantumCircuit(self.nqubit)
                
        angle_y = np.arcsin(x)
        angle_z = np.arccos(x**2)

        for i in range(self.nqubit):
            if i % 2 == 0:
                u.add_RY_gate(i, angle_y[0])
                u.add_RZ_gate(i, angle_z[0])
            else:
                u.add_RY_gate(i, angle_y[1])
                u.add_RZ_gate(i, angle_z[1])
        
        return u
    def create_input_gate(self, x):
        # Encode x into quantum state
        # x = 2-dim. variables, [-1,1]
        u = QuantumCircuit(self.nqubit)

        angle_y = np.arcsin(x)
        angle_z = np.arccos(x**2)

        for i in range(self.nqubit):
            '''
            if i % 2 == 0:
                u.add_RY_gate(i, angle_y[0])
                u.add_RZ_gate(i, angle_z[0])
            else:
                u.add_RY_gate(i, angle_y[1])
                u.add_RZ_gate(i, angle_z[1])
            '''
            u.add_RY_gate(i, angle_y[i])
            u.add_RZ_gate(i, angle_z[i])
        return u
示例#6
0
    def _try_append_gate(self, op: ops.GateOperation,
                         qulacs_circuit: qulacs.QuantumCircuit,
                         indices: np.array):
        # One qubit gate
        if isinstance(op.gate, ops.pauli_gates._PauliX):
            qulacs_circuit.add_X_gate(indices[0])
        elif isinstance(op.gate, ops.pauli_gates._PauliY):
            qulacs_circuit.add_Y_gate(indices[0])
        elif isinstance(op.gate, ops.pauli_gates._PauliZ):
            qulacs_circuit.add_Z_gate(indices[0])
        elif isinstance(op.gate, ops.common_gates.HPowGate):
            qulacs_circuit.add_H_gate(indices[0])
        elif isinstance(op.gate, ops.common_gates.XPowGate):
            qulacs_circuit.add_RX_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.common_gates.YPowGate):
            qulacs_circuit.add_RY_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.common_gates.ZPowGate):
            qulacs_circuit.add_RZ_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.SingleQubitMatrixGate):
            mat = op.gate._matrix
            qulacs_circuit.add_dense_matrix_gate(indices[0], mat)
        elif isinstance(op.gate, circuits.qasm_output.QasmUGate):
            lmda = op.gate.lmda
            theta = op.gate.theta
            phi = op.gate.phi
            gate = qulacs.gate.U3(indices[0], theta * np.pi, phi * np.pi,
                                  lmda * np.pi)
            qulacs_circuit.add_gate(gate)

        # Two qubit gate
        elif isinstance(op.gate, ops.common_gates.CNotPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_CNOT_gate(indices[0], indices[1])
            else:
                mat = _get_google_rotx(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[1], mat)
                gate.add_control_qubit(indices[0], 1)
                qulacs_circuit.add_gate(gate)
        elif isinstance(op.gate, ops.common_gates.CZPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_CZ_gate(indices[0], indices[1])
            else:
                mat = _get_google_rotz(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[1], mat)
                gate.add_control_qubit(indices[0], 1)
                qulacs_circuit.add_gate(gate)
        elif isinstance(op.gate, ops.common_gates.SwapPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_SWAP_gate(indices[0], indices[1])
            else:
                qulacs_circuit.add_dense_matrix_gate(indices, op._unitary_())
        elif isinstance(op.gate, ops.parity_gates.XXPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [1, 1], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.parity_gates.YYPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [2, 2], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.parity_gates.ZZPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [3, 3], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.TwoQubitMatrixGate):
            indices.reverse()
            mat = op.gate._matrix
            qulacs_circuit.add_dense_matrix_gate(indices, mat)

            # Three qubit gate
            """
            # deprecated because these functions cause errors in gpu
            elif isinstance(op.gate, ops.three_qubit_gates.CCXPowGate):
                mat = _get_google_rotx(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[2], mat)
                gate.add_control_qubit(indices[0],1)
                gate.add_control_qubit(indices[1],1)
                qulacs_circuit.add_gate(gate)
            elif isinstance(op.gate, ops.three_qubit_gates.CCZPowGate):
                mat = _get_google_rotz(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[2], mat)
                gate.add_control_qubit(indices[0],1)
                gate.add_control_qubit(indices[1],1)
                qulacs_circuit.add_gate(gate)
            """
        elif isinstance(op.gate, ops.three_qubit_gates.CSwapGate):
            mat = np.zeros(shape=(4, 4))
            mat[0, 0] = 1
            mat[1, 2] = 1
            mat[2, 1] = 1
            mat[3, 3] = 1
            gate = qulacs.gate.DenseMatrix(indices[1:], mat)
            gate.add_control_qubit(indices[0], 1)
            qulacs_circuit.add_gate(gate)

        # Misc
        elif protocols.has_unitary(op):
            indices.reverse()
            mat = op._unitary_()
            qulacs_circuit.add_dense_matrix_gate(indices, mat)

        # Not unitary
        else:
            return False

        return True
示例#7
0
    def create_input_gate(self, x, uin_type):
        # Encode x into quantum state
        # uin_type: unitary data-input type 0, 1, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61
        # x = 1dim. variables, [-1,1]

        I_mat = np.eye(2, dtype=complex)
        X_mat = X(0).get_matrix()
        Y_mat = Y(0).get_matrix()
        Z_mat = Z(0).get_matrix()

        #make operators s.t. exp(i*theta * sigma^z_j@sigma^z_k)   @:tensor product
        def ZZ(u, theta, j, k):
            u.add_CNOT_gate(j, k)
            u.add_RZ_gate(k, -2 * theta * self.time_step)
            u.add_CNOT_gate(j, k)
            return u

        def XX(u, theta, j, k):
            u.add_H_gate(j)
            u.add_H_gate(k)
            ZZ(u, theta, j, k)
            u.add_H_gate(j)
            u.add_H_gate(k)
            return u

        def YY(u, theta, j, k):
            u.add_U1_gate(j, -np.pi / 2.)
            u.add_U1_gate(k, -np.pi / 2.)
            XX(u, theta, j, k)
            u.add_U1_gate(j, np.pi / 2.)
            u.add_U1_gate(k, np.pi / 2.)
            return u

        theta = x

        u = QuantumCircuit(self.nqubit)

        angle_y = np.arcsin(x)
        angle_z = np.arccos(x**2)

        if uin_type == 0:
            for i in range(self.nqubit):
                u.add_RY_gate(i, angle_y[i])
                u.add_RZ_gate(i, angle_z[i])

        elif uin_type == 1:
            #for d in range(2):
            for i in range(self.nqubit):
                u.add_H_gate(i)
                u.add_RY_gate(i, angle_y[i])
                u.add_RZ_gate(i, angle_z[i])
            # KT: add second order expansion
            for i in range(self.nqubit - 1):
                for j in range(i + 1, self.nqubit):
                    angle_z2 = np.arccos(x[i] * x[j])
                    u.add_CNOT_gate(i, j)
                    u.add_RZ_gate(j, angle_z2)
                    u.add_CNOT_gate(i, j)

        elif uin_type == 20:
            for i in range(self.nqubit):
                u.add_RX_gate(i, -2 * x[i] * self.time_step)

        elif uin_type == 21:
            ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex)
            for i in range(self.nqubit):  # i runs 0 to nqubit-1
                J_x = x[i]
                print(x)
                ham += J_x * make_fullgate([[i, X_mat]], self.nqubit)

            ## Build time-evolution operator by diagonalizing the Ising hamiltonian H*P = P*D <-> H = P*D*P^dagger
            diag, eigen_vecs = np.linalg.eigh(ham)
            time_evol_op = np.dot(
                np.dot(eigen_vecs,
                       np.diag(np.exp(-1j * self.time_step * diag))),
                eigen_vecs.T.conj())  # e^-iHT

            # Convert to qulacs gate
            time_evol_gate = DenseMatrix([i for i in range(self.nqubit)],
                                         time_evol_op)
            u.add_gate(time_evol_gate)

        elif uin_type == 30:
            #Ising hamiltonian with input coefficient
            # nearest neighbor spin-conbination has interaction
            for i in range(self.nqubit):
                u.add_RX_gate(i, -2 * x[i] * self.time_step)
                ZZ(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1)

        elif uin_type == 31:

            ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex)
            for i in range(self.nqubit):
                J_x = x[i]
                ham += J_x * make_fullgate([[i, X_mat]], self.nqubit)
                J_zz = x[i] * x[(i + 1) % self.nqubit]
                ham += J_zz * make_fullgate(
                    [[i, Z_mat], [(i + 1) % self.nqubit, Z_mat]], self.nqubit)

            diag, eigen_vecs = np.linalg.eigh(ham)
            time_evol_op = np.dot(
                np.dot(eigen_vecs,
                       np.diag(np.exp(-1j * self.time_step * diag))),
                eigen_vecs.T.conj())

            time_evol_gate = DenseMatrix([i for i in range(self.nqubit)],
                                         time_evol_op)
            u.add_gate(time_evol_gate)

        elif uin_type == 40:
            #Ising hamiltonian with input coefficient
            # every two possible spin-conbination has interaction
            for i in range(self.nqubit):
                u.add_RX_gate(i, -2 * x[i] * self.time_step)
                for j in range(i + 1, self.nqubit):
                    ZZ(u, theta[i] * theta[j], i, j)

        elif uin_type == 41:

            ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex)
            for i in range(self.nqubit):
                J_x = x[i]
                ham += J_x * make_fullgate([[i, X_mat]], self.nqubit)
                for j in range(i + 1, self.nqubit):
                    J_ij = x[i] * x[j]
                    ham += J_ij * make_fullgate([[i, Z_mat], [j, Z_mat]],
                                                self.nqubit)

            diag, eigen_vecs = np.linalg.eigh(ham)
            time_evol_op = np.dot(
                np.dot(eigen_vecs,
                       np.diag(np.exp(-1j * self.time_step * diag))),
                eigen_vecs.T.conj())

            time_evol_gate = DenseMatrix([i for i in range(self.nqubit)],
                                         time_evol_op)
            u.add_gate(time_evol_gate)

        elif uin_type == 50:
            #Heisenberg hamiltonian with input coefficient
            # nearest neighbor spin-conbination has interaction
            for i in range(self.nqubit):
                u.add_RX_gate(i, -2 * x[i] * self.time_step)
                XX(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1)
                YY(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1)
                ZZ(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1)

        elif uin_type == 51:

            ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex)
            for i in range(self.nqubit):
                J_x = x[i]
                ham += J_x * make_fullgate([[i, X_mat]], self.nqubit)

                J_xx = x[i] * x[(i + 1) % self.nqubit]
                J_yy = x[i] * x[(i + 1) % self.nqubit]
                J_zz = x[i] * x[(i + 1) % self.nqubit]
                ham += J_xx * make_fullgate(
                    [[i, X_mat], [(i + 1) % self.nqubit, X_mat]], self.nqubit)
                ham += J_yy * make_fullgate(
                    [[i, Y_mat], [(i + 1) % self.nqubit, Y_mat]], self.nqubit)
                ham += J_xx * make_fullgate(
                    [[i, Z_mat], [(i + 1) % self.nqubit, Z_mat]], self.nqubit)

            diag, eigen_vecs = np.linalg.eigh(ham)
            time_evol_op = np.dot(
                np.dot(eigen_vecs,
                       np.diag(np.exp(-1j * self.time_step * diag))),
                eigen_vecs.T.conj())

            time_evol_gate = DenseMatrix([i for i in range(self.nqubit)],
                                         time_evol_op)
            u.add_gate(time_evol_gate)

        elif uin_type == 60:
            #Heisenberg hamiltonian with input coefficient
            # every two possible spin-conbination has interaction
            for i in range(self.nqubit):
                u.add_RX_gate(i, -2 * x[i] * self.time_step)
                for j in range(i + 1, self.nqubit):
                    XX(u, theta[i] * theta[j], i, j)
                    YY(u, theta[i] * theta[j], i, j)
                    ZZ(u, theta[i] * theta[j], i, j)

        elif uin_type == 61:

            ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex)
            for i in range(self.nqubit):
                J_x = x[i]
                ham += J_x * make_fullgate([[i, X_mat]], self.nqubit)

                for j in range(i + 1, self.nqubit):
                    J_xx = x[i] * x[j]
                    J_yy = x[i] * x[j]
                    J_zz = x[i] * x[j]
                    ham += J_xx * make_fullgate([[i, X_mat], [j, X_mat]],
                                                self.nqubit)
                    ham += J_yy * make_fullgate([[i, Y_mat], [j, Y_mat]],
                                                self.nqubit)
                    ham += J_xx * make_fullgate([[i, Z_mat], [j, Z_mat]],
                                                self.nqubit)

            diag, eigen_vecs = np.linalg.eigh(ham)
            time_evol_op = np.dot(
                np.dot(eigen_vecs,
                       np.diag(np.exp(-1j * self.time_step * diag))),
                eigen_vecs.T.conj())

            time_evol_gate = DenseMatrix([i for i in range(self.nqubit)],
                                         time_evol_op)
            u.add_gate(time_evol_gate)

        else:
            pass

        return u