Beispiel #1
0
 def test_init_4(self):
     """test '__new__' (seed)
     """
     sb = Stabilizer(gene_num=3, qubit_num=3, seed=123)
     actual = sb.get_str()
     expect = "  III\n  III\n  III\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #2
0
 def test_cx_14(self):
     """test 'CX' (input:ZX)
     """
     sb = Stabilizer(gene_num=1, qubit_num=2)
     sb.set_pauli_op(0, 0, 'Z').set_pauli_op(0, 1, 'X')
     sb.cx(0, 1)
     actual = sb.get_str()
     expect = "  ZX\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #3
0
 def test_s_dg_4(self):
     """test 'S+' (input:I)
     """
     sb = Stabilizer(gene_num=1, qubit_num=1)
     sb.set_pauli_op(0, 0, 'I')
     sb.s_dg(0)
     actual = sb.get_str()
     expect = "  I\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #4
0
 def test_h_2(self):
     """test 'H' (input:Y)
     """
     sb = Stabilizer(gene_num=1, qubit_num=1)
     sb.set_pauli_op(0, 0, 'Y')
     sb.h(0)
     actual = sb.get_str()
     expect = " -Y\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #5
0
 def test_cy_15(self):
     """test 'CY' (input:ZY)
     """
     sb = Stabilizer(gene_num=1, qubit_num=2)
     sb.set_pauli_op(0, 0, 'Z').set_pauli_op(0, 1, 'Y')
     sb.cy(0, 1)
     actual = sb.get_str()
     expect = "  ZY\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #6
0
 def test_z_1(self):
     """test 'Z' (input:X)
     """
     sb = Stabilizer(gene_num=1, qubit_num=1)
     sb.set_pauli_op(0, 0, 'X')
     sb.z(0)
     actual = sb.get_str()
     expect = " -X\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #7
0
 def test_get_pauli_fac_2(self):
     """test 'get_pauli_fac'
     """
     sb = Stabilizer(gene_num=3, qubit_num=4)
     sb.set_all('Y').set_pauli_op(1, 2, 'Y')
     sb.set_pauli_fac(1, '-i')
     actual = sb.get_pauli_fac(1)
     expect = -1j
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #8
0
 def test_cz_7(self):
     """test 'CZ' (input:IZ)
     """
     sb = Stabilizer(gene_num=1, qubit_num=2)
     sb.set_pauli_op(0, 0, 'I').set_pauli_op(0, 1, 'Z')
     sb.cz(0, 1)
     actual = sb.get_str()
     expect = "  IZ\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #9
0
 def test_s_3(self):
     """test 'S' (input:Z)
     """
     sb = Stabilizer(gene_num=1, qubit_num=1)
     sb.set_pauli_op(0, 0, 'Z')
     sb.s(0)
     actual = sb.get_str()
     expect = "  Z\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #10
0
 def test_my_1(self):
     """test 'my'
     """
     sb = Stabilizer(gene_num=2, qubit_num=2)
     sb.set_all('Z')
     sb.h(0).cx(0, 1)
     md = sb.my(qid=[0, 1], shots=10)
     frq = md.frequency
     lst = md.last
     ans = ((lst == '01' or lst == '10') and (frq['01'] + frq['10'] == 10))
     sb.free()
     self.assertEqual(ans, True)
Beispiel #11
0
 def test_mx_2(self):
     """test 'mx'
     """
     sb = Stabilizer(gene_num=3, qubit_num=3)
     sb.set_all('Z')
     sb.h(0).cx(0, 1).cx(0, 2)
     md = sb.mx(qid=[0, 1, 2], shots=10)
     frq = md.frequency
     lst = md.last
     ans = ((lst == '000' or lst == '011' or lst == '101' or lst == '110')
            and (frq['000'] + frq['011'] + frq['101'] + frq['110'] == 10))
     self.assertEqual(ans, True)
     sb.free()
Beispiel #12
0
 def test_reset_1(self):
     """test 'clone'
     """
     sb = Stabilizer(gene_num=4, qubit_num=3)
     sb.set_all('Y')
     sb_clone = sb.clone()
     actual = sb_clone.get_str()
     expect = sb.get_str()
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #13
0
 def test_set_pauli_fac_2(self):
     """test 'set_pauli_fac'
     """
     sb = Stabilizer(gene_num=3, qubit_num=4)
     sb.set_all('Z').set_pauli_op(1, 0, 'Y').set_pauli_op(1, 2, 'Y')
     actual = sb.set_pauli_fac(1, '-i').get_str()
     expect = "  ZIII\n-iYZYI\n  IIZI\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #14
0
 def test_set_all_3(self):
     """test 'set_all' (gene_num > qubit_num, 'X')
     """
     sb = Stabilizer(gene_num=3, qubit_num=2)
     sb.set_all('X')
     actual = sb.get_str()
     expect = "  XI\n  IX\n  II\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #15
0
 def test_reset_1(self):
     """test 'reset'
     """
     sb = Stabilizer(gene_num=4, qubit_num=3)
     sb.set_all('Y').reset()
     actual = sb.get_str()
     expect = "  III\n  III\n  III\n  III\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #16
0
 def test_set_all_5(self):
     """test 'set_all' (gene_num = qubit_num, 'Z')
     """
     sb = Stabilizer(qubit_num=3)
     sb.set_all('Z')
     actual = sb.get_str()
     expect = "  ZII\n  IZI\n  IIZ\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #17
0
    def __init__(self, qubit_num, cmem_num=0, backend=None):

        self.qubit_num = qubit_num
        self.cmem_num = cmem_num
        self.cmem = [0] * cmem_num
        self.qcirc = []

        if backend is None:
            self.backend = Backend('qlazy_qstate_simulator')
        else:
            self.backend = backend

        # qlazy qstate simulator
        if self.backend.name == 'qlazy_qstate_simulator':
            self.qstate = QState(qubit_num=qubit_num)
        else:
            self.qstate = None

        # qlazy stabilizer simulator
        if self.backend.name == 'qlazy_stabilizer_simulator':
            self.stab = Stabilizer(qubit_num=qubit_num)
            self.stab.set_all('Z')
        else:
            self.stab = None
Beispiel #18
0
class QComp:
    """ Quantum Computer.

    Attributes
    ----------
    qubit_num : int
        size of qubits
    cmem_num : int
        size of classical memory
    cmem : list
        classical memory
    qcirc : dict
        quantum circuit
    qstate : instance of QState
        quantum state (for 'qlazy_qstate_simulator')
    stab : instance of Stabilizer
        stabilizer group (for 'qlazy_stabilizer_simulator')

    """
    def __init__(self, qubit_num, cmem_num=0, backend=None):

        self.qubit_num = qubit_num
        self.cmem_num = cmem_num
        self.cmem = [0] * cmem_num
        self.qcirc = []

        if backend is None:
            self.backend = Backend('qlazy_qstate_simulator')
        else:
            self.backend = backend

        # qlazy qstate simulator
        if self.backend.name == 'qlazy_qstate_simulator':
            self.qstate = QState(qubit_num=qubit_num)
        else:
            self.qstate = None

        # qlazy stabilizer simulator
        if self.backend.name == 'qlazy_stabilizer_simulator':
            self.stab = Stabilizer(qubit_num=qubit_num)
            self.stab.set_all('Z')
        else:
            self.stab = None

    def reset(self, reset_qubits=True, reset_cmem=True, reset_qcirc=True):

        if reset_qubits == True:
            if self.qstate != None:
                self.qstate.reset()
            if self.stab != None:
                self.stab.set_all('Z')

        if reset_cmem == True:
            del self.cmem
            self.cmem = [0] * self.cmem_num

        if reset_qcirc == True:
            del self.qcirc
            self.qcirc = []

    def free(self):
        self.reset()

        if self.qstate != None:
            self.qstate.free()
        elif self.stab != None:
            self.stab.free()

    def run(self,
            shots=DEF_SHOTS,
            reset_qubits=True,
            reset_cmem=True,
            reset_qcirc=True):
        """
        run the quantum circuit.

        Parameters
        ----------
        shots : int, default 1
            number of measurements.

        Returns
        -------
        result : dict
            measurement result.

        Examples
        --------
        >>> qc = QComp(2).h(0).cx(0,1).measure(qid=[0,1])  # add quantum gates, set circuit
        >>> result = qc.run(shots=100)  # run the circuit, get measured result
        >>> print(result)
        {'measured_qid': [0,1], 'frequency': Counter({'00': 5, '11': 5})}

        """
        if self.backend.name == 'qlazy_qstate_simulator':
            result = run_qlazy_qstate_simulator(self.qstate,
                                                self.qcirc,
                                                self.cmem,
                                                shots=shots)
            self.reset(reset_qubits, reset_cmem, reset_qcirc)
        elif self.backend.name == 'qlazy_stabilizer_simulator':
            result = run_qlazy_stabilizer_simulator(self.stab,
                                                    self.qcirc,
                                                    self.cmem,
                                                    shots=shots)
            self.reset(reset_qubits, reset_cmem, reset_qcirc)
        else:
            raise QComp_Error_BackendNotSupported()

        return result

    def measure(self, qid, cid=None, ctrl=None):
        """
        add measurement gate (Z-basis).

        Parameters
        ----------
        qid : list of int
            qubit id list to measure.
        cid : list of int
            classical register id list to store measured result.
        ctrl : int
            address of classical memory to control gate operation

        Returns
        -------
        self : instance of QCirc

        Notes
        -----
        'cid' must be 'None' or same length as 'qid'

        """
        self.__add_quantum_gate(kind=MEASURE, qid=qid, cid=cid, ctrl=ctrl)
        return self

    # add 1-qubit gate

    def x(self, q0, ctrl=None):
        """
        add X gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PAULI_X, qid=[q0], ctrl=ctrl)
        return self

    def y(self, q0, ctrl=None):
        """
        add Y gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp

        """
        self.__add_quantum_gate(kind=PAULI_Y, qid=[q0], ctrl=ctrl)
        return self

    def z(self, q0, ctrl=None):
        """
        add Z gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PAULI_Z, qid=[q0], ctrl=ctrl)
        return self

    def h(self, q0, ctrl=None):
        """
        add H gate (hadamard gate).

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=HADAMARD, qid=[q0])
        return self

    def xr(self, q0, ctrl=None):
        """
        add root X gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=ROOT_PAULI_X, qid=[q0], ctrl=ctrl)
        return self

    def xr_dg(self, q0, ctrl=None):
        """
        add root X dagger gate 
        (hermmitian conjugate of root X gate).

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=ROOT_PAULI_X_, qid=[q0], ctrl=ctrl)
        return self

    def s(self, q0, ctrl=None):
        """
        add S gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PHASE_SHIFT_S, qid=[q0], ctrl=ctrl)
        return self

    def s_dg(self, q0, ctrl=None):
        """
        add S dagger gate (hermitian conjugate of S gate).

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PHASE_SHIFT_S_, qid=[q0], ctrl=ctrl)
        return self

    def t(self, q0, ctrl=None):
        """
        add T gate.

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PHASE_SHIFT_T, qid=[q0], ctrl=ctrl)
        return self

    def t_dg(self, q0, ctrl=None):
        """
        add T dagger gate (hermitian conjugate of T gate).

        Parameters
        ----------
        q0 : int
            qubit id.
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=PHASE_SHIFT_T_, qid=[q0], ctrl=ctrl)
        return self

    def rx(self, q0, phase=DEF_PHASE, ctrl=None):
        """
        add RX gate (rotation around X-axis).

        Parameters
        ----------
        q0 : int
            qubit id.
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QState

        """
        self.__add_quantum_gate(kind=ROTATION_X,
                                qid=[q0],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def ry(self, q0, phase=DEF_PHASE, ctrl=None):
        """
        add RY gate (rotation around Y-axis).

        Parameters
        ----------
        q0 : int
            qubit id.
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=ROTATION_Y,
                                qid=[q0],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def rz(self, q0, phase=DEF_PHASE, ctrl=None):
        """
        add RZ gate (rotation around Z-axis).

        Parameters
        ----------
        q0 : int
            qubit id.
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=ROTATION_Z,
                                qid=[q0],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def p(self, q0, phase=DEF_PHASE, ctrl=None):
        """
        add P gate (phase shift gate).

        Parameters
        ----------
        q0 : int
            qubit id.
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        Notes
        -----
        matrix expression is following...
        | 1.0 0.0             |
        | 0.0 exp(i*phase*PI) |

        """
        self.__add_quantum_gate(kind=PHASE_SHIFT,
                                qid=[q0],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def u1(self, q0, alpha=DEF_PHASE, ctrl=None):
        """
        add U1 gate (by IBM).

        Parameters
        ----------
        q0 : int
            qubit id.
        alpha : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        Notes
        -----
        this opration is equal to P gate (phase shift gate)

        """
        self.__add_quantum_gate(kind=ROTATION_U1,
                                qid=[q0],
                                phase=alpha,
                                ctrl=ctrl)
        return self

    def u2(self, q0, alpha=DEF_PHASE, beta=DEF_PHASE, ctrl=None):
        """
        add U2 gate (by IBM).

        Parameters
        ----------
        q0 : int
            qubit id.
        alpha : float
            rotation angle (unit of angle is pi radian).
        beta : float
            rotation angle (unit of angle is pi radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        Notes
        -----
        matrix experssion is following...
        | 1/sqrt(2)              -exp(i*alpha*PI)/sqrt(2)       |
        | exp(i*beta*PI)/sqrt(2) exp(i*(alpha+beta)*PI)/sqrt(2) |

        """
        self.__add_quantum_gate(kind=ROTATION_U2,
                                qid=[q0],
                                phase=alpha,
                                phase1=beta,
                                ctrl=ctrl)
        return self

    def u3(self,
           q0,
           alpha=DEF_PHASE,
           beta=DEF_PHASE,
           gamma=DEF_PHASE,
           ctrl=None):
        """
        add U3 gate (by IBM).

        Parameters
        ----------
        q0 : int
            qubit id.
        alpha : float
            rotation angle (unit of angle is pi radian).
        beta : float
            rotation angle (unit of angle is pi radian).
        gamma : float
            rotation angle (unit of angle is pi radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        Notes
        -----
        matrix expression is following...
        | cos(gamma/2)                -exp(i*alpha*PI)*sin(gamma/2)       |
        | exp(i*beta*PI)*sin(gamma/2) exp(i*(alpha+beta)*PI)*cos(gamma/2) |


        """
        self.__add_quantum_gate(kind=ROTATION_U3,
                                qid=[q0],
                                phase=alpha,
                                phase1=beta,
                                phase2=gamma,
                                ctrl=ctrl)
        return self

    # add 2-qubit gate

    def cx(self, q0, q1, ctrl=None):
        """
        add CX gate (controlled X gate, controlled NOT gate, CNOT gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_X, qid=[q0, q1], ctrl=ctrl)
        return self

    def cy(self, q0, q1, ctrl=None):
        """
        operate CY gate (controlled X gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QState

        """
        self.__add_quantum_gate(kind=CONTROLLED_Z, qid=[q0, q1], ctrl=ctrl)
        self.__add_quantum_gate(kind=CONTROLLED_X, qid=[q0, q1], ctrl=ctrl)
        self.__add_quantum_gate(kind=PHASE_SHIFT_S, qid=[q0], ctrl=ctrl)
        return self

    def cz(self, q0, q1, ctrl=None):
        """
        add CZ gate (controlled Z gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_Z, qid=[q0, q1], ctrl=ctrl)
        return self

    def cxr(self, q0, q1, ctrl=None):
        """
        add CXR gate (controlled root X gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_XR, qid=[q0, q1], ctrl=ctrl)
        return self

    def cxr_dg(self, q0, q1, ctrl=None):
        """
        add CXR dagger gate (controlled XR dagger gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_XR_, qid=[q0, q1], ctrl=ctrl)
        return self

    def ch(self, q0, q1, ctrl=None):
        """
        add CH gate (controlled H gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_H, qid=[q0, q1], ctrl=ctrl)
        return self

    def cs(self, q0, q1, ctrl=None):
        """
        add CS gate (controlled S gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_S, qid=[q0, q1], ctrl=ctrl)
        return self

    def cs_dg(self, q0, q1, ctrl=None):
        """
        add CS dagger gate (controlled S dagger gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_S_, qid=[q0, q1], ctrl=ctrl)
        return self

    def ct(self, q0, q1, ctrl=None):
        """
        add CT gate (controlled T gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_T, qid=[q0, q1], ctrl=ctrl)
        return self

    def ct_dg(self, q0, q1, ctrl=None):
        """
        add CT dagger gate (controlled T dagger gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_T_, qid=[q0, q1], ctrl=ctrl)
        return self

    def sw(self, q0, q1, ctrl=None):
        """
        add swap gate

        Parameters
        ----------
        q0 : int
            qubit id
        q1 : int
            qubit id
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=SWAP, qid=[q0, q1], ctrl=ctrl)
        return self

    def cp(self, q0, q1, phase=DEF_PHASE, ctrl=None):
        """
        add CP gate (controlled P gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_P,
                                qid=[q0, q1],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def crx(self, q0, q1, phase=DEF_PHASE, ctrl=None):
        """
        add CRX gate (controlled RX gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_RX,
                                qid=[q0, q1],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def cry(self, q0, q1, phase=DEF_PHASE, ctrl=None):
        """
        add CRY gate (controlled RY gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_RY,
                                qid=[q0, q1],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def crz(self, q0, q1, phase=DEF_PHASE, ctrl=None):
        """
        add CRZ gate (controlled RZ gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        phase : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_RZ,
                                qid=[q0, q1],
                                phase=phase,
                                ctrl=ctrl)
        return self

    def cu1(self, q0, q1, alpha=DEF_PHASE, ctrl=None):
        """
        add CU1 gate (controlled U1 gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        alpha : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_U1,
                                qid=[q0, q1],
                                phase=alpha,
                                ctrl=ctrl)
        return self

    def cu2(self, q0, q1, alpha=DEF_PHASE, beta=DEF_PHASE, ctrl=None):
        """
        add CU2 gate (controlled U2 gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        alpha : float
            rotation angle (unit of angle is PI radian).
        beta : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_U2,
                                qid=[q0, q1],
                                phase=alpha,
                                phase1=beta,
                                ctrl=ctrl)
        return self

    def cu3(self,
            q0,
            q1,
            alpha=DEF_PHASE,
            beta=DEF_PHASE,
            gamma=DEF_PHASE,
            ctrl=None):
        """
        add CU3 gate (controlled U3 gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (target qubit).
        alpha : float
            rotation angle (unit of angle is PI radian).
        beta : float
            rotation angle (unit of angle is PI radian).
        gamma : float
            rotation angle (unit of angle is PI radian).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.__add_quantum_gate(kind=CONTROLLED_U3,
                                qid=[q0, q1],
                                phase=alpha,
                                phase1=beta,
                                phase2=gamma,
                                ctrl=ctrl)
        return self

    # 3-qubit gate

    def ccx(self, q0, q1, q2, ctrl=None):
        """
        add CCX gate (toffoli gate, controlled controlled X gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (control qubit).
        q2 : int
            qubit id (target qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.cxr(q1, q2, ctrl=ctrl).cx(q0, q1, ctrl=ctrl).cxr_dg(q1,
                                                                 q2,
                                                                 ctrl=ctrl)
        self.cx(q0, q1, ctrl=ctrl).cxr(q0, q2, ctrl=ctrl)
        return self

    def csw(self, q0, q1, q2, ctrl=None):
        """
        add CSW gate (fredkin gate, controlled swap gate).

        Parameters
        ----------
        q0 : int
            qubit id (control qubit).
        q1 : int
            qubit id (swap qubit).
        q2 : int
            qubit id (swap qubit).
        ctrl : int
            address of classical memory to control gate operation.

        Returns
        -------
        self : instance of QComp.

        """
        self.cx(q2, q1, ctrl=ctrl).ccx(q0, q1, q2, ctrl=ctrl).cx(q2,
                                                                 q1,
                                                                 ctrl=ctrl)
        return self

    def __add_quantum_gate(self,
                           kind=None,
                           qid=None,
                           cid=None,
                           phase=DEF_PHASE,
                           phase1=DEF_PHASE,
                           phase2=DEF_PHASE,
                           ctrl=None):

        if cid != None and len(qid) != len(cid):
            raise QComp_Error_NumberOfClassicalReg()

        # non-clifford operation is not allowed for stabilizer calculation
        if (is_clifford_gate(kind) is False
                and is_measurement_gate(kind) is False
                and self.backend.name == 'qlazy_stabilizer_simulator'):
            raise QComp_Error_QgateNotSupported()

        else:
            self.qcirc.append({
                'kind': kind,
                'qid': qid,
                'cid': cid,
                'phase': phase,
                'phase1': phase1,
                'phase2': phase2,
                'ctrl': ctrl
            })
Beispiel #19
0
def operate_logical_cnot(lq, shots=5):

    # set lattice
    lattice_row, lattice_col = 7, 8
    lattice = create_lattice(lattice_row, lattice_col)

    # make vacuum state
    qubit_num = (2 * lattice_row + 1) * (2 * lattice_col + 1)
    sb = Stabilizer(qubit_num=qubit_num, gene_num=qubit_num + 1)
    initialize(sb, lattice)

    # set logical qubit #0
    p0_pos_A, p0_pos_B = [0, 0], [0, 1]
    p0_path = [[0, 1], [0, 2]]
    move_defect(sb, p0_pos_A, p0_pos_B, p0_path, 'p', lattice, create=True)
    if lq[0] == '-': operate_logical_Z(sb, 0, lattice)

    # set logical qubit #1
    d1_pos_A, d1_pos_B = [2, 5], [3, 5]
    d1_path = [[3, 5], [4, 5], [5, 5]]
    move_defect(sb, d1_pos_A, d1_pos_B, d1_path, 'd', lattice, create=True)

    # set logical qubit #2
    p2_pos_A, p2_pos_B = [0, 7], [1, 7]
    p2_path = [[1, 7], [2, 7], [3, 7]]
    move_defect(sb, p2_pos_A, p2_pos_B, p2_path, 'p', lattice, create=True)

    # set logical qubit #3
    p3_pos_A, p3_pos_B = [6, 0], [6, 1]
    p3_path = [[6, 1], [6, 2]]
    move_defect(sb, p3_pos_A, p3_pos_B, p3_path, 'p', lattice, create=True)
    if lq[1] == '-': operate_logical_Z(sb, 3, lattice)

    # braid logical qubit #0
    p0_pos_A, p0_pos_B = [0, 0], [0, 2]
    p0_path = [[0, 2], [1, 2], [2, 2], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6],
               [2, 6], [1, 6], [0, 6], [0, 5], [0, 4], [0, 3], [0, 2]]
    move_defect(sb, p0_pos_A, p0_pos_B, p0_path, 'p', lattice)

    # braid logical qubit #2
    p2_pos_A, p2_pos_B = [0, 7], [3, 7]
    p2_path = [[3, 7], [3, 6], [3, 5], [3, 4], [3, 3], [4, 3], [5, 3], [6, 3],
               [6, 4], [6, 5], [6, 6], [6, 7], [5, 7], [4, 7], [3, 7]]
    move_defect(sb, p2_pos_A, p2_pos_B, p2_path, 'p', lattice)

    # braid and annihilate logical qubit #3
    p3_pos_A, p3_pos_B = [6, 0], [6, 2]
    p3_path = [[6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [5, 6], [4, 6], [3, 6],
               [3, 5], [3, 4], [3, 3], [3, 2], [4, 2], [5, 2], [6, 2], [6, 1]]
    mval_p = move_defect(sb,
                         p3_pos_A,
                         p3_pos_B,
                         p3_path,
                         'p',
                         lattice,
                         annihilate=True)

    # braid and annihilate logical qubit #1
    d1_pos_A, d1_pos_B = [2, 5], [5, 5]
    d1_path = [[5, 5], [4, 5], [3, 5]]
    mval_d = move_defect(sb,
                         d1_pos_A,
                         d1_pos_B,
                         d1_path,
                         'd',
                         lattice,
                         annihilate=True)

    if mval_p == '1':
        operate_logical_Z(sb, 0, lattice)
        operate_logical_Z(sb, 2, lattice)

    # measure logical qubits: #0 and #2
    chain_0 = get_chain(get_path([0, 0], [0, 2]), 'p', lattice)
    chain_2 = get_chain(get_path([0, 7], [3, 7]), 'p', lattice)
    freq = measure_logical_X(sb, chain_0, chain_2, shots=shots)
    print("Input('{0:}') == [CNOT] ==> {1:}".format(lq, freq))

    sb.free()
Beispiel #20
0
        mval = (str(sum([int(s) for s in list(mval_0)]) % 2) +
                str(sum([int(s) for s in list(mval_1)]) % 2))
        mval_list.append(mval)
        sb_tmp.free()
    return Counter(mval_list)


if __name__ == '__main__':

    lattice_row = 4
    lattice_col = 6
    lattice = create_lattice(lattice_row, lattice_col)

    # make vacuum state
    qubit_num = (2 * lattice_row + 1) * (2 * lattice_col + 1)
    sb = Stabilizer(qubit_num=qubit_num)
    initialize(sb, lattice)

    # logical qubit #1
    d_pos_A = [2, 1]
    d_pos_B = [2, 2]
    d_path = [[2, 2], [2, 3], [2, 4]]
    create_move_defect_d(sb, d_pos_A, d_pos_B, d_path, lattice)

    # logical qubit #0
    p_pos_A = [0, 0]
    p_pos_B = [0, 1]
    # p_path = [[0,1],[0,2]]
    p_path = [[0, 1], [0, 2], [1, 2], [2, 2], [3, 2], [3, 3], [3, 4], [3, 5],
              [2, 5], [1, 5], [0, 5], [0, 4], [0, 3], [0, 2]]
    create_move_defect_p(sb, p_pos_A, p_pos_B, p_path, lattice)
Beispiel #21
0
 def test_set_pauli_op_1(self):
     """test 'set_pauli_op'
     """
     sb = Stabilizer(gene_num=4, qubit_num=3)
     sb.set_pauli_op(0, 0, 'X').set_pauli_op(0, 1,
                                             'Y').set_pauli_op(0, 2, 'Z')
     sb.set_pauli_op(1, 0, 'Y').set_pauli_op(1, 1,
                                             'Z').set_pauli_op(1, 2, 'X')
     sb.set_pauli_op(2, 0, 'Z').set_pauli_op(2, 1,
                                             'X').set_pauli_op(2, 2, 'Y')
     sb.set_pauli_op(3, 0, 'I').set_pauli_op(3, 1,
                                             'I').set_pauli_op(3, 2, 'I')
     actual = sb.get_str()
     expect = "  XYZ\n  YZX\n  ZXY\n  III\n"
     sb.free()
     self.assertEqual(actual, expect)
Beispiel #22
0
 def test_get_pauli_op_2(self):
     """test 'get_pauli_op'
     """
     sb = Stabilizer(gene_num=4, qubit_num=3)
     sb.set_pauli_op(0, 0, 'X').set_pauli_op(0, 1,
                                             'Y').set_pauli_op(0, 2, 'Z')
     sb.set_pauli_op(1, 0, 'Y').set_pauli_op(1, 1,
                                             'Z').set_pauli_op(1, 2, 'X')
     sb.set_pauli_op(2, 0, 'Z').set_pauli_op(2, 1,
                                             'X').set_pauli_op(2, 2, 'Y')
     sb.set_pauli_op(3, 0, 'I').set_pauli_op(3, 1,
                                             'I').set_pauli_op(3, 2, 'I')
     sb.set_pauli_fac(0, '+1').set_pauli_fac(1, '-1')
     sb.set_pauli_fac(2, '+i').set_pauli_fac(3, '-i')
     actual = sb.get_pauli_op(3, 2)
     expect = "I"
     sb.free()
     self.assertEqual(actual, expect)