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)
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)
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_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)
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)
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 evaluate(qc, qs, verbose=False): """ evaluate """ # backend shots = 1000 bk = Backend(product='ibmq', device='aer_simulator') res = bk.run(qcirc=qc, shots=shots) prob_aws = freq2prob(res.frequency) # qlazy prob_qlz = qs.get_prob() if verbose is True: print("prob_qlz =", prob_qlz) print("prob_aws =", prob_aws) value = kl_divergence(prob_qlz, prob_aws) if verbose is True: print("value =", value) if value >= EPS: print("value =", value) return value
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_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_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)
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_x(self): """test 'x' gate """ bk = Backend(product='qlazy', device='stabilizer_simulator') qc = QCirc().x(0) res = bk.run(qcirc=qc, out_state=True) actual = res.stabilizer.get_str() expect = " -Z\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_hh_cz(self): """test 'CZ' (folowint 'h' gate) """ bk = Backend(product='qlazy', device='stabilizer_simulator') qc = QCirc().h(0).h(1).cz(0, 1) res = bk.run(qcirc=qc, out_state=True) actual = res.stabilizer.get_str() expect = " XZ\n ZX\n" self.assertEqual(actual, expect)
def test_inheritance(self): """test 'inheritance' """ bk = Backend(product='qulacs', device='gpu_simulator') qc = MyQCirc().bell(0,1).measure(qid=[0,1], cid=[0,1]) res = bk.run(qcirc=qc, shots=10) freq = res.frequency ans = (freq['00']+freq['11'] == 10) and (freq['00'] != 0) and (freq['11'] != 0) self.assertEqual(ans, 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_x(self): """test 'x' gate """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().x(0) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0.0, 1.0]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def valid_or_not(qc): bk = Backend(product='ibmq', device='aer_simulator') res = bk.run(qcirc=qc) qasm_A = res.info['quantum_circuit'].qasm() qasm_B = qc.to_qasm() circ_A = Circuit.from_qasm(qasm_A) circ_B = Circuit.from_qasm(qasm_B) ans = circ_A.verify_equality(circ_B) return ans
def test_h_t_dg(self): """test 't_dg' gate (following 'h' gate) """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().h(0).t_dg(0) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([1.0 / SQRT_2, 0.5 - 0.5j]) ans = equal_vectors(actual, expect) self.assertEqual(ans, True)
def test_h_rz(self): """test 'rz' gate (following 'h' gate) """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().h(0).rz(0, phase=0.25) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0.65328148 - 0.27059805j, 0.65328148 + 0.27059805j]) ans = equal_vectors(actual, expect) self.assertEqual(ans, True)
def test_h_y(self): """test 'y' gate (following 'h' gate) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().h(0).y(0) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([-1.0j/SQRT_2, 1.0j/SQRT_2]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def test_x_sw(self): """test 'sw' gate (following 'x' gate, not 'h' gates) """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().x(0).sw(0, 1) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0j, (1 + 0j), 0j, 0j]) ans = equal_vectors(actual, expect) self.assertEqual(ans, True)
def test_p(self): """test 'p' gate """ bk = Backend(product='qlazy', device='qstate_gpu_simulator') qc = QCirc().p(0, phase=0.25) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([1.0, 0.0]) ans = equal_vectors(actual, expect) self.assertEqual(ans, True)
def test_measure_mesurement_only_3(self): """test 'measure' (measurement only (3)) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().measure(qid=[0,1], cid=[0,1]) res = bk.run(qcirc=qc, shots=10) freq = res.frequency cid = res.cid self.assertEqual(freq['00'], 10) self.assertEqual(cid, [0,1])
def test_measure_unitary_measuremen_cunitary_measurement(self): """test 'measure' (unitary-measurement-cunitary-measurement) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().h(0).cx(0,1).measure(qid=[0], cid=[0]).x(0, ctrl=0).x(1, ctrl=0).measure(qid=[0,1], cid=[0,1]) res = bk.run(qcirc=qc, shots=10) freq = res.frequency cid = res.cid self.assertEqual(freq['00'], 10) self.assertEqual(cid, [0,1])
def test_ry(self): """test 'ry' gate """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().ry(0, phase=0.25) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([COS_PI_8, SIN_PI_8]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def test_h_ry(self): """test 'ry' gate (following 'h' gate) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().h(0).ry(0, phase=0.25) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0.38268343+0.j, 0.92387953+0.j]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def test_x_x_csw(self): """test 'csw' gate """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().x(0).x(1).csw(0,1,2) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0j, 0j, 0j, 0j, 0j, (1+0j), 0j, 0j]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def test_h_p(self): """test 'p' gate (following 'h' gate) """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().h(0).p(0, phase=0.25) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([0.70710678, 0.5+0.5j]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)
def test_sw(self): """test 'sw' gate """ bk = Backend(product='qulacs', device='gpu_simulator') qc = QCirc().h(0).h(1).sw(0,1) res = bk.run(qcirc=qc, out_state=True) actual = res.qstate.amp expect = np.array([(0.5+0j), (0.5+0j), (0.5+0j), (0.5+0j)]) ans = equal_vectors(actual, expect) self.assertEqual(ans,True)