Beispiel #1
0
 def test_get_add_gates(self):
     """test get_gates, add_gates
     """
     qc_A = QCirc().x(0).z(2).h(0).h(0).cx(0, 1).h(0).h(1).crz(
         1, 0, phase=0.1).rx(1, phase=0.2)
     gates = qc_A.get_gates()
     qc_B = QCirc().add_gates(gates)
     self.assertEqual(qc_A, qc_B)
Beispiel #2
0
 def test_merge_3terms(self):
     """test 'merge' (2 terms)
     """
     qc_1 = QCirc().h(0).cx(0, 1).rx(1, phase=0.2)
     qc_2 = QCirc().cry(0, 1, phase=0.3).measure(qid=[0, 1, 2],
                                                 cid=[0, 1, 2])
     qc_3 = QCirc().x(0).z(5)
     qc_123 = QCirc().h(0).cx(0, 1).rx(1, phase=0.2).cry(
         0, 1, phase=0.3).measure(qid=[0, 1, 2], cid=[0, 1, 2]).x(0).z(5)
     self.assertEqual(qc_1 + qc_2 + qc_3 == qc_123, True)
Beispiel #3
0
 def test_merge_2terms(self):
     """test 'merge' (2 terms)
     """
     qc_L = QCirc().h(0).cx(0, 1).rx(1, phase=0.2)
     qc_R = QCirc().crx(0, 1, phase=0.3).measure(qid=[0, 1, 2],
                                                 cid=[0, 1, 2])
     qc_LR = QCirc().h(0).cx(0, 1).rx(1, phase=0.2).crx(
         0, 1, phase=0.3).measure(qid=[0, 1, 2], cid=[0, 1, 2])
     self.assertEqual(qc_L + qc_R == qc_LR, True)
     self.assertEqual(qc_R + qc_L == qc_LR, False)
Beispiel #4
0
 def test_kind_first(self):
     """test 'kind_first'
     """
     qc_L = QCirc().h(0).cx(0, 1).rx(1, phase=0.2)
     qc_R = QCirc().crz(0, 1, phase=0.3).measure(qid=[0, 1, 2],
                                                 cid=[0, 1, 2])
     qc_LR = QCirc().h(0).cx(0, 1).rx(1, phase=0.2).cry(
         0, 1, phase=0.3).measure(qid=[0, 1, 2], cid=[0, 1, 2])
     self.assertEqual(qc_L.kind_first(), HADAMARD)
     self.assertEqual(qc_R.kind_first(), CONTROLLED_RZ)
     self.assertEqual(qc_LR.kind_first(), HADAMARD)
 def test_operate_controlled_xyz(self):
     """test 'operate' (controlled_xyz)
     """
     bk = Backend(product='qlazy', device='stabilizer_simulator')
     qc = QCirc().cx(3, 2).cy(3, 0).cz(3, 1)
     res = bk.run(qcirc=qc, out_state=True)
     expect = res.stabilizer.get_str()
     pp = PauliProduct(pauli_str="XYZ", qid=[2, 0, 1])
     qc = QCirc().operate(pp=pp, ctrl=3)
     res = bk.run(qcirc=qc, out_state=True)
     actual = res.stabilizer.get_str()
     self.assertEqual(expect, actual)
Beispiel #6
0
 def test_is_equal(self):
     """test 'is_equal'
     """
     qc_L = QCirc().h(0).cx(0, 1).rx(1, phase=0.2)
     qc_R = QCirc().crx(0, 1, phase=0.3).measure(qid=[0, 1, 2],
                                                 cid=[0, 1, 2])
     qc_L_clone = qc_L.clone()
     ans = (qc_L == qc_R)
     self.assertEqual(qc_L == qc_R, False)
     self.assertEqual(qc_R != qc_L, True)
     self.assertEqual(qc_L == qc_L_clone, True)
     self.assertEqual(qc_L != qc_L_clone, False)
 def test_operate_h_z(self):
     """test 'operate' (Z followed by h)
     """
     bk = Backend(product='qlazy', device='stabilizer_simulator')
     qc = QCirc().h(0).z(0)
     res = bk.run(qcirc=qc, out_state=True)
     expect = res.stabilizer.get_str()
     pp = PauliProduct(pauli_str="Z")
     qc = QCirc().h(0).operate(pp=pp)
     res = bk.run(qcirc=qc, out_state=True)
     actual = res.stabilizer.get_str()
     self.assertEqual(expect, actual)
Beispiel #8
0
 def test_operate_controlled_xyz(self):
     """test 'operate' (controlled_xyz)
     """
     bk = Backend(product='qulacs', device='gpu_simulator')
     qc = QCirc().cx(3,2).cy(3,0).cz(3,1)
     res = bk.run(qcirc=qc, out_state=True)
     expect = res.qstate.amp
     pp = PauliProduct(pauli_str="XYZ", qid=[2,0,1])
     qc = QCirc().operate(pp=pp, ctrl=3)
     res = bk.run(qcirc=qc, out_state=True)
     actual = res.qstate.amp
     ans = equal_vectors(actual, expect)
     self.assertEqual(ans,True)
Beispiel #9
0
 def test_merge_incremental(self):
     """test 'merge' (incremental)
     """
     qc_1 = QCirc().h(0).cx(0, 1).rx(1, phase=0.2)
     qc_2 = QCirc().cry(0, 1, phase=0.3).measure(qid=[0, 1, 2],
                                                 cid=[0, 1, 2])
     qc_3 = QCirc().x(0).z(5)
     qc_expect = QCirc().h(0).cx(0, 1).rx(1, phase=0.2).cry(
         0, 1, phase=0.3).measure(qid=[0, 1, 2], cid=[0, 1, 2]).x(0).z(5)
     qc_actual = qc_1.clone()
     qc_actual += qc_2
     qc_actual += qc_3
     self.assertEqual(qc_actual == qc_expect, True)
Beispiel #10
0
 def test_operate_h_z(self):
     """test 'operate' (Z followed by h)
     """
     bk = Backend(product='qulacs', device='gpu_simulator')
     qc = QCirc().h(0).z(0)
     res = bk.run(qcirc=qc, out_state=True)
     expect = res.qstate.amp
     pp = PauliProduct(pauli_str="Z")
     qc = QCirc().h(0).operate(pp=pp)
     res = bk.run(qcirc=qc, out_state=True)
     actual = res.qstate.amp
     ans = equal_vectors(actual, expect)
     self.assertEqual(ans,True)
Beispiel #11
0
 def test_operate_xyz(self):
     """test 'operate' (xyz)
     """
     bk = Backend(product='qlazy', device='qstate_gpu_simulator')
     qc = QCirc().x(2).y(0).z(1)
     res = bk.run(qcirc=qc, out_state=True)
     expect = res.qstate.amp
     pp = PauliProduct(pauli_str="XYZ", qid=[2, 0, 1])
     qc = QCirc().operate(pp=pp)
     res = bk.run(qcirc=qc, out_state=True)
     actual = res.qstate.amp
     ans = equal_vectors(actual, expect)
     self.assertEqual(ans, True)
Beispiel #12
0
 def test_h_p_h(self):
     """test 'h-p-h' gate
     """
     qc = QCirc().h(0).p(0, phase=0.1).h(0).measure(qid=[0], cid=[0])
     qs = QState(qubit_num=1).h(0).p(0, phase=0.1).h(0)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #13
0
 def test_init(self):
     """test '__new__'
     """
     qc = QCirc()
     self.assertEqual(qc.qubit_num, 0)
     self.assertEqual(qc.cmem_num, 0)
     self.assertEqual(qc.gate_num, 0)
Beispiel #14
0
 def test_ryy(self):
     """test 'ryy' gate
     """
     qc = QCirc().h(0).h(1).ryy(0, 1, phase=0.1).measure(qid=[0, 1], cid=[0, 1])
     qs = QState(qubit_num=2).h(0).h(1).ryy(0, 1, phase=0.1)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #15
0
 def test_csw(self):
     """test 'csw' gate
     """
     qc = QCirc().x(0).x(1).csw(0, 1, 2).measure(qid=[0, 1, 2], cid=[0, 1, 2])
     qs = QState(qubit_num=3).x(0).x(1).csw(0, 1, 2)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #16
0
 def test_xr_dg(self):
     """test 'xr_dg' gate
     """
     qc = QCirc().xr_dg(0).measure(qid=[0], cid=[0])
     qs = QState(qubit_num=1).xr_dg(0)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #17
0
 def test_h_z(self):
     """test 'h-z' gate
     """
     qc = QCirc().h(0).z(0).measure(qid=[0], cid=[0])
     qs = QState(qubit_num=1).h(0).z(0)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #18
0
 def test_x_sw(self):
     """test 'x-sw' gate
     """
     qc = QCirc().x(0).sw(0, 1).measure(qid=[0, 1], cid=[0, 1])
     qs = QState(qubit_num=2).x(0).sw(0, 1)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #19
0
 def test_ct_dg(self):
     """test 'ct_dg' gate
     """
     qc = QCirc().h(0).h(1).ct_dg(0, 1).measure(qid=[0, 1], cid=[0, 1])
     qs = QState(qubit_num=2).h(0).h(1).ct_dg(0, 1)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #20
0
 def test_append_gate_simple(self):
     """test 'append_gate' (simple)
     """
     qc = QCirc().h(0).cx(0, 1).rx(3, phase=0.1)
     self.assertEqual(qc.qubit_num, 4)
     self.assertEqual(qc.cmem_num, 0)
     self.assertEqual(qc.gate_num, 3)
Beispiel #21
0
 def test_reset(self):
     """test reset
     """
     qc = QCirc().reset(qid=[0, 1])
     actual = qc.to_qasm()
     expect = """OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\nreset q[0];\nreset q[1];"""
     self.assertEqual(actual, expect)
Beispiel #22
0
 def test_append_gate_with_cid(self):
     """test 'append_gate' (with ctrl)
     """
     qc = QCirc().h(0).cx(0, 1, ctrl=5).rx(3, phase=0.1)
     self.assertEqual(qc.qubit_num, 4)
     self.assertEqual(qc.cmem_num, 6)
     self.assertEqual(qc.gate_num, 3)
Beispiel #23
0
 def test_rz(self):
     """test 'rz' gate
     """
     qc = QCirc().rz(0, phase=0.1).measure(qid=[0], cid=[0])
     qs = QState(qubit_num=1).rz(0, phase=0.1)
     value = evaluate(qc, qs)
     self.assertEqual(value < EPS, True)
Beispiel #24
0
def main():

    shots = 100
    qc = QCirc()
    qc.h(0).x(0).z(0).s(0).t(0).s_dg(0).t_dg(0).rx(0, phase=0.1).rz(0, phase=0.2)
    qc.cx(0,1).cz(0,1).ch(0,1).crz(0,1, phase=0.3)
    qc.measure(qid=[0,1], cid=[0,1]) # {'00': 50, '10': 25, '11': 25}
    # qc.measure(qid=[0], cid=[0]) # {'0': 50, '1': 59}
    # qc.measure(qid=[1], cid=[1]) # {'00': 75, '01': 25}
    # qc.measure(qid=[0,1], cid=[1,0]) # {'00': 50, '01': 25, '11': 25}
    # qc.measure(qid=[0,1], cid=[1,3]) # {'0000': 50, '0100': 25, '0101': 25}
    # qc.measure(qid=[0,1], cid=[3,1]) # {'0000': 50, '0001': 25, '0101': 25}
    
    bk = Backend()  # OK

    # bk = Backend(product='ibmq', device='aer_simulator') # OK
    # bk = Backend(product='ibmq', device='qasm_simulator') # OK
    # bk = Backend(product='qulacs', device='cpu_simulator') # OK

    # bk = Backend(product='braket_local', device='braket_sv') # OK
    # bk = Backend(product='braket_aws', device='sv1') # OK
    # bk = Backend(product='braket_aws', device='tn1') # OK
    # bk = Backend(product='braket_aws', device='dm1') # OK

    # bk = Backend(product='braket_ionq', device='ionq') # OK
    # bk = Backend(product='braket_rigetti', device='aspen_11')
    # bk = Backend(product='braket_rigetti', device='aspen_m_1') # OK
    # bk = Backend(product='braket_oqc', device='lucy') # OK

    result = bk.run(qcirc=qc, shots=shots)
    result.show(verbose=True)
Beispiel #25
0
 def test_save_load(self):
     """test save, load
     """
     qc_A = QCirc().x(0).z(2).h(0).h(0).cx(0, 1).h(0).h(1).crz(
         1, 0, phase=0.1).rx(1, phase=0.2)
     qc_A.save("tmp/foo.qc")
     qc_B = QCirc.load("tmp/foo.qc")
     self.assertEqual(qc_A, qc_B)
Beispiel #26
0
 def test_many_unitary_gates(self):
     """test many unitary gates
     """
     qc_A = QCirc().h(0).cx(0, 1).crx(1, 0, phase=0.1).csw(0, 1, 2)
     str_A = qc_A.to_qasm()
     qc_B = QCirc.from_qasm(str_A)
     str_B = qc_B.to_qasm()
     self.assertEqual(str_A, str_B)
Beispiel #27
0
 def test_csw(self):
     """test csw
     """
     qc_A = QCirc().csw(0, 1, 2)
     str_A = qc_A.to_qasm()
     qc_B = QCirc.from_qasm(str_A)
     str_B = qc_B.to_qasm()
     self.assertEqual(str_A, str_B)
Beispiel #28
0
 def test_crz(self):
     """test crz
     """
     qc_A = QCirc().crz(0, 1, phase=0.1)
     str_A = qc_A.to_qasm()
     qc_B = QCirc.from_qasm(str_A)
     str_B = qc_B.to_qasm()
     self.assertEqual(str_A, str_B)
Beispiel #29
0
 def test_ct_dg(self):
     """test ct_dg
     """
     qc_A = QCirc().ct_dg(0, 1)
     str_A = qc_A.to_qasm()
     qc_B = QCirc.from_qasm(str_A)
     str_B = qc_B.to_qasm()
     self.assertEqual(str_A, str_B)
Beispiel #30
0
 def test_cxr_2(self):
     """test cxr (2)
     """
     qc_A = QCirc().h(0).cxr(0, 1)
     str_A = qc_A.to_qasm()
     qc_B = QCirc.from_qasm(str_A)
     str_B = qc_B.to_qasm()
     self.assertEqual(str_A, str_B)