Ejemplo n.º 1
0
def qstate_print(qs, qid=None, nonzero=False):
    """ print quantum state vector """

    if qid is None or qid == []:
        qid = list(range(qs.qubit_num))

    qstate_check_args(qs, kind=cfg.SHOW, qid=qid)

    try:
        qubit_num = len(qid)
        qubit_id = [0 for _ in range(qubit_num)]
        for i, q in enumerate(qid):
            qubit_id[i] = q

        IntArray = ctypes.c_int * qubit_num
        qid_array = IntArray(*qubit_id)

        lib.qstate_print.restype = ctypes.c_bool
        lib.qstate_print.argtypes = [ctypes.POINTER(QState), ctypes.c_int, IntArray, ctypes.c_bool]
        ret = lib.qstate_print(ctypes.byref(qs), ctypes.c_int(qubit_num),
                               qid_array, ctypes.c_bool(nonzero))

        if ret is False:
            raise ValueError("can't print quantum state vector.")

    except Exception:
        raise ValueError("can't print quantum state vector.")
Ejemplo n.º 2
0
def qstate_measure_bell_stats(qs, qid=None, shots=cfg.DEF_SHOTS):
    """ bell measurement of the qubits and get stats """

    if qid is None or qid == []:
        qid = list(range(2))

    # error check
    qstate_check_args(qs, kind=cfg.MEASURE_BELL, qid=qid)

    # operate
    qubit_num = 2
    qubit_id = [0 for _ in range(qubit_num)]
    for i in range(qubit_num):
        qubit_id[i] = qid[i]
    IntArray = ctypes.c_int * qubit_num
    qid_array = IntArray(*qubit_id)

    mdata = None
    c_mdata = ctypes.c_void_p(mdata)

    lib.qstate_measure_bell_stats.restype = ctypes.c_bool
    lib.qstate_measure_bell_stats.argtypes = [ctypes.POINTER(QState), ctypes.c_int,
                                              ctypes.c_int, IntArray,
                                              ctypes.POINTER(ctypes.c_void_p)]
    ret = lib.qstate_measure_bell_stats(ctypes.byref(qs), ctypes.c_int(shots),
                                        ctypes.c_int(qubit_num), qid_array, c_mdata)

    if ret is False:
        raise ValueError("can't measure the qubits.")

    out = ctypes.cast(c_mdata.value, ctypes.POINTER(MData))

    return out.contents
Ejemplo n.º 3
0
def qstate_bloch(qs, q=0):
    """ get bloch angle from quantum state vector """

    # error check
    qstate_check_args(qs, kind=cfg.BLOCH, qid=[q])

    try:
        theta = 0.0
        phi = 0.0
        c_theta = ctypes.c_double(theta)
        c_phi = ctypes.c_double(phi)

        lib.qstate_bloch.restype = ctypes.c_bool
        lib.qstate_bloch.argtypes = [ctypes.POINTER(QState), ctypes.c_int,
                                     ctypes.POINTER(ctypes.c_double),
                                     ctypes.POINTER(ctypes.c_double)]
        ret = lib.qstate_bloch(ctypes.byref(qs), ctypes.c_int(q),
                               ctypes.byref(c_theta), ctypes.byref(c_phi))

        if ret is False:
            raise ValueError("can't get bloch angle.")

        theta = c_theta.value
        phi = c_phi.value

        return theta, phi

    except Exception:
        raise ValueError("can't get bloch angle.")
Ejemplo n.º 4
0
def qstate_operate_qgate(qs, kind=None, qid=None, phase=cfg.DEF_PHASE,
                         phase1=cfg.DEF_PHASE, phase2=cfg.DEF_PHASE):
    """ operate quantum gate to the quantum state """

    # error check
    qstate_check_args(qs, kind=kind, qid=qid)

    qubit_id = [-1 for _ in range(2)]
    for i, q in enumerate(qid):
        qubit_id[i] = q
    IntArray = ctypes.c_int * 2
    qid_array = IntArray(*qubit_id)

    lib.qstate_operate_qgate.restype = ctypes.c_bool
    lib.qstate_operate_qgate.argtypes = [ctypes.POINTER(QState), ctypes.c_int,
                                         ctypes.c_double, ctypes.c_double,
                                         ctypes.c_double, IntArray]
    ret = lib.qstate_operate_qgate(ctypes.byref(qs), ctypes.c_int(kind),
                                   ctypes.c_double(phase), ctypes.c_double(phase1),
                                   ctypes.c_double(phase2), qid_array)

    if ret is False:
        raise ValueError("can't operate quantum gate to the quantum state vector.")