def test_operate_controlled_xyz(self): """test 'operate' (controlled_xyz) """ bk = Backend(product='qlazy', device='qstate_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)
def test_operate_x(self): """test 'operate' (x) """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().x(0) res = bk.run(qcirc=qc, out_state=True) expect = res.qstate.amp pp = PauliProduct(pauli_str="X") 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)
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)
def test_operate_xyz(self): """test 'operate' (xyz) """ bk = Backend(product='qulacs', device='cpu_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)
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)
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_1(self): qid_0 = CreateRegister(2) qid_1 = CreateRegister(3,4) qubit_num = InitRegister(qid_0, qid_1) cid_0 = CreateRegister(4) cid_1 = CreateRegister(2,3) cmem_num = InitRegister(cid_0, cid_1) bk = Backend(product='qlazy', device='qstate_simulator') qc = QCirc() qc.h(qid_0[1]).cx(qid_0[1], qid_1[0][2]).measure(qid=[qid_0[1], qid_1[0][2]], cid=[cid_1[0][0],cid_1[1][1]]) res = bk.run(qcirc=qc, shots=10, cid=[cid_1[0][0],cid_1[1][1]]) freq = res.frequency self.assertEqual(freq['00']+freq['11'], 10)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def test_operate_xyz(self): """test 'operate' (xyz) """ pp = PauliProduct(pauli_str="XYZ") qc = QCirc().operate(pp=pp).measure(qid=[0, 1, 2], cid=[0, 1, 2]) qs = QState(qubit_num=3).operate(pp=pp) value = evaluate(qc, qs) self.assertEqual(value < EPS, True)
def test_reset_unitary_measure_reset(self): """test 'reset' (unitary-measure-reset) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().x(0).x(1).measure(qid=[0,1,2], cid=[0,1,2]).reset(qid=[1]).measure(qid=[0,1], cid=[0,1]) res = bk.run(qcirc=qc, shots=10, cid=[0,1]) freq = res.frequency self.assertEqual(freq['10'], 10)
def test_operate_controlled_xyz(self): """test 'operate' (xyz) """ pp = PauliProduct(pauli_str="XYZ", qid=[1, 2, 3]) qc = QCirc().operate(pp=pp, ctrl=0).measure(qid=[0, 1, 2, 3], cid=[0, 1, 2, 3]) qs = QState(qubit_num=4).operate(pp=pp, ctrl=0) value = evaluate(qc, qs) self.assertEqual(value < EPS, True)
def main(): qc = QCirc.generate_random_gates(qubit_num=5, gate_num=10, phase=(0.0, 0.25, 0.5), prob={ 'h': 7, 'cx': 5, 'rx': 3, 'crz': 3 }) qc += QCirc().measure(qid=list(range(5)), cid=list(range(5))) qc += QCirc().h(0).x(0, ctrl=3).h(1).measure(qid=[2, 1], cid=[10, 4]) print(qc) print() qc.show()
def test_pop_gate_updte(self): """test 'pop_gate' (update) """ qc = QCirc().h(5, ctrl=6).cx(0, 1).rx(1, phase=0.2).crz( 0, 1, phase=0.3).measure(qid=[0, 1, 2], cid=[0, 1, 2]) self.assertEqual(qc.qubit_num, 6) self.assertEqual(qc.cmem_num, 7) self.assertEqual(qc.gate_num, 7) (kind, qid, para, c, ctrl) = qc.pop_gate() self.assertEqual(kind, HADAMARD) self.assertEqual(qid, [5, -1]) self.assertEqual(para, [0.0, 0.0, 0.0]) self.assertEqual(c, None) self.assertEqual(ctrl, 6) self.assertEqual(qc.qubit_num, 3) self.assertEqual(qc.cmem_num, 3) self.assertEqual(qc.gate_num, 6)
def test_operate_h_z(self): """test 'operate' (h-z) """ pp = PauliProduct(pauli_str="Z") qc = QCirc().h(0).operate(pp=pp).measure(qid=[0], cid=[0]) qs = QState(qubit_num=1).h(0).operate(pp=pp) value = evaluate(qc, qs) self.assertEqual(value < EPS, True)
def test_reset_simple_partial(self): """test 'reset' (simple_partial) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().x(0).x(1).reset(qid=[1]).measure(qid=[0,1], cid=[0,1]) res = bk.run(qcirc=qc, shots=10) freq = res.frequency self.assertEqual(freq['10'], 10)
def test_append_gate_with_cid(self): """test 'append_gate' (with cid) """ qc = QCirc().h(0).cx(0, 1).rx(3, phase=0.1).measure(qid=[0, 5], cid=[1, 2]) self.assertEqual(qc.qubit_num, 6) self.assertEqual(qc.cmem_num, 3) self.assertEqual(qc.gate_num, 5)
def test_h_y(self): """test 'y' gate (following 'h' gate) """ bk = Backend(product='qlazy', device='stabilizer_simulator') qc = QCirc().h(0).y(0) res = bk.run(qcirc=qc, out_state=True) actual = res.stabilizer.get_str() expect = " -X\n" self.assertEqual(actual, expect)
def test_csw(self): """test csw """ qc = QCirc().csw(0, 1, 2) bk = Backend() qs_A = bk.run(qcirc=qc, out_state=True).qstate qs_B = QState(qubit_num=qc.qubit_num).csw(0, 1, 2) self.assertEqual(equal_or_not(qs_A, qs_B), True) self.assertEqual(valid_or_not(qc), True)
def test_rzz(self): """test rzz """ qc = QCirc().rzz(0, 1, phase=0.1) bk = Backend() qs_A = bk.run(qcirc=qc, out_state=True).qstate qs_B = QState(qubit_num=qc.qubit_num).rzz(0, 1, phase=0.1) self.assertEqual(equal_or_not(qs_A, qs_B), True) self.assertEqual(valid_or_not(qc), True)
def test_reset_simple_all(self): """test 'reset' (simple_all) """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().x(0).x(1).reset(qid=[0, 1, 2]).measure(qid=[0, 1, 2], cid=[0, 1, 2]) res = bk.run(qcirc=qc, shots=10) freq = res.frequency self.assertEqual(freq['000'], 10)