def test_u1_gate(backend): assert is_vec_same(Circuit().u1(-1.23)[1].run(backend=backend), Circuit().u3(0, 0, -1.23)[1].run(backend=backend))
def test_rotation1(backend): assert np.allclose( Circuit().ry(-math.pi / 2)[0].rz(math.pi / 6)[0].ry( math.pi / 2)[0].run(backend=backend), Circuit().rx(math.pi / 6)[0].run(backend=backend))
def test_globalphase_rz(backend): theta = 1.2 c = Circuit().rz(theta)[0] assert abs(c.run(backend=backend)[0] - cmath.exp(-0.5j * theta)) < 1e-8
def test_sdg_gate(backend): assert np.allclose(Circuit().s[1].sdg[1].run(backend=backend), Circuit().i[1].run(backend=backend))
def test_u_gate(backend): assert np.allclose( Circuit().u(1.23, 4.56, -5.43, -0.5 * (4.56 - 5.43))[1].run(backend=backend), Circuit().rz(-5.43)[1].ry(1.23)[1].rz(4.56)[1].run(backend=backend))
def test_cx6(backend): '''Refer issues #76 (https://github.com/Blueqat/Blueqat/issues/76)''' c = Circuit().x[0].cx[0, 3].cx[3, 1] result = np.zeros(16) result[11] = 1.0 assert np.allclose(c.run(backend=backend), result)
def test_sgate(backend): assert np.allclose( Circuit().s[0].run(backend=backend), ignore_global_phase(Circuit().rz(math.pi / 2)[0].run(backend=backend)))
def test_caching_then_expand(backend): c = Circuit().h[0] c.run(backend=backend) qubits = c.i[1].run(backend=backend) assert is_vec_same(qubits, Circuit().h[0].i[1].run())
def test_hgate1(backend): assert is_vec_same(Circuit().h[1].h[0].run(backend=backend), np.array([0.5, 0.5, 0.5, 0.5]))
def test_measurement_multiqubit1(backend): c = Circuit().x[0].m[1] cnt = c.run(backend=backend, shots=10000) # 0-th qubit is also 0 because it is not measured. assert cnt.most_common(1) == [("00", 10000)]
def test_measurement_multiqubit2(backend): c = Circuit().x[0].m[1::-1] cnt = c.run(backend=backend, shots=10000) assert cnt.most_common(1) == [("10", 10000)]
def test_measurement2(backend): c = Circuit().x[0].m[0] cnt = c.run(backend=backend, shots=10000) assert cnt.most_common(1) == [("1", 10000)]
def test_crotation2(backend): assert is_vec_same( Circuit().crx(1.23)[1, 3].run(backend=backend), Circuit().h[3].crz(1.23)[1, 3].h[3].run(backend=backend))
def test_rotation1(backend): assert is_vec_same(Circuit().ry(-np.pi / 2)[0].rz(np.pi / 6)[0].ry( np.pi / 2)[0].run(backend=backend), Circuit().rx(np.pi / 6)[0].run(backend=backend), ignore_global='ab')
def test_tgate(backend): assert is_vec_same(Circuit().t[0].run(backend=backend), Circuit().rz(np.pi / 4)[0].run(backend=backend), ignore_global='ab')
def test_hgate2(backend): assert is_vec_same(Circuit().x[0].h[0].run(backend=backend), np.array([1 / np.sqrt(2), -1 / np.sqrt(2)]))
assert c.run(shots=5) == c.run_with_numpy(shots=5) def test_macro(): def macro(c, i): return c.h[i] BlueqatGlobalSetting.register_macro('foo', macro) try: assert np.allclose(Circuit().foo(1).run(), Circuit().h[1].run()) finally: BlueqatGlobalSetting.unregister_macro('foo') @pytest.mark.parametrize('pair', [ (Circuit(10).x[:].reset[:], Circuit(10)), (Circuit(10).h[:].reset[:], Circuit(10)), ]) def test_reset1(backend, pair): if backend == 'qgate': pytest.xfail('Reset gate for qgate is unimplemented.') assert np.allclose(pair[0].run(backend=backend), pair[1].run(backend=backend)) def test_reset2(backend): if backend == 'qgate': pytest.xfail('Reset gate for qgate is unimplemented.') common = Circuit().h[0].cx[0, 1].cx[0, 2].reset[1].m[:].run( backend=backend, shots=100).most_common(3) assert len(common) == 2
def test_hgate3(backend): assert is_vec_same(Circuit().h[:2].run(backend=backend), Circuit().h[0].h[1].run(backend=backend))
def test_rz2(backend): assert np.allclose( Circuit().h[0].rz(math.pi / 3)[0].h[1].rz(math.pi / 3)[1].run(backend=backend), Circuit().h[0, 1].rz(math.pi / 3)[:].run(backend=backend))
def test_pauli3(backend): assert is_vec_same(Circuit().z[0].x[0].run(backend=backend), Circuit().y[0].run(backend=backend), ignore_global='ab')
def test_tdg_gate(backend): assert np.allclose( ignore_global_phase(Circuit().s[1].tdg[1].tdg[1].run(backend=backend)), Circuit().i[1].run(backend=backend))
def test_cx1(backend): assert is_vec_same( Circuit().h[0].h[1].cx[1, 0].h[0].h[1].run(backend=backend), Circuit().cx[0, 1].run(backend=backend))
def test_y_gate(backend): assert np.allclose(Circuit().y[1].run(backend=backend), Circuit().z[1].x[1].run(backend=backend) * 1j)
def test_cx2(backend): assert is_vec_same(Circuit().x[2].cx[:4:2, 1:4:2].run(backend=backend), Circuit().x[2:4].run(backend=backend))
def test_r_gate(backend): assert np.allclose(Circuit().r(-1.23)[1].run(backend=backend), Circuit().u(0, 0, -1.23)[1].run(backend=backend))
def test_rz1(backend): assert is_vec_same(Circuit().h[0].rz(np.pi)[0].run(backend=backend), Circuit().x[0].h[0].run(backend=backend))
def test_crotation2(backend): assert np.allclose( Circuit().crx(1.23)[1, 3].run(backend=backend), Circuit().h[3].crz(1.23)[1, 3].h[3].run(backend=backend))
def test_rz2(backend): assert is_vec_same( Circuit().h[0].rz(np.pi / 3)[0].h[1].rz(np.pi / 3)[1].run(backend=backend), Circuit().h[0, 1].rz(np.pi / 3)[:].run(backend=backend))
def test_globalphase_r(backend): theta = 1.2 c = Circuit().phase(theta)[0] assert abs(c.run(backend=backend)[0] - 1) < 1e-8
def test_u2_gate(backend): assert is_vec_same( Circuit().u2(-1.23, 4.56)[1].run(backend=backend), Circuit().u3(np.pi / 2, -1.23, 4.56)[1].run(backend=backend))