Exemplo n.º 1
0
def test_isclose_rel_tol():
    a = qo.QubitOperator('X0', 1)
    b = qo.QubitOperator('X0', 2)
    assert a.isclose(b, rel_tol=2.5, abs_tol=0.1)
    # Test symmetry
    assert a.isclose(b, rel_tol=1, abs_tol=0.1)
    assert b.isclose(a, rel_tol=1, abs_tol=0.1)
Exemplo n.º 2
0
def test_imul_qubit_op_2():
    op3 = qo.QubitOperator(((1, 'Y'), (0, 'X')), -1j)
    op4 = qo.QubitOperator(((1, 'Y'), (0, 'X'), (2, 'Z')), -1.5)
    op3 *= op4
    op4 *= op3
    assert ((2, 'Z'), ) in op3.terms
    assert op3.terms[((2, 'Z'), )] == 1.5j
Exemplo n.º 3
0
def test_or_one_qubit():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend, engine_list=[])
    qureg = eng.allocate_qureg(3)
    eng.flush()
    identity = qo.QubitOperator("", 1j)
    x = qo.QubitOperator("X1", cmath.exp(0.5j))
    y = qo.QubitOperator("Y2", cmath.exp(0.6j))
    z = qo.QubitOperator("Z0", cmath.exp(4.5j))
    identity | qureg
    eng.flush()
    x | qureg
    eng.flush()
    y | qureg
    eng.flush()
    z | qureg
    eng.flush()
    assert saving_backend.received_commands[4].gate == Ph(math.pi / 2.)

    assert saving_backend.received_commands[6].gate == X
    assert saving_backend.received_commands[6].qubits == ([qureg[1]], )
    assert saving_backend.received_commands[7].gate == Ph(0.5)
    assert saving_backend.received_commands[7].qubits == ([qureg[1]], )

    assert saving_backend.received_commands[9].gate == Y
    assert saving_backend.received_commands[9].qubits == ([qureg[2]], )
    assert saving_backend.received_commands[10].gate == Ph(0.6)
    assert saving_backend.received_commands[10].qubits == ([qureg[2]], )

    assert saving_backend.received_commands[12].gate == Z
    assert saving_backend.received_commands[12].qubits == ([qureg[0]], )
    assert saving_backend.received_commands[13].gate == Ph(4.5)
    assert saving_backend.received_commands[13].qubits == ([qureg[0]], )
Exemplo n.º 4
0
def test_isclose_different_terms():
    a = qo.QubitOperator(((1, 'Y'), ), -0.1j)
    b = qo.QubitOperator(((1, 'X'), ), -0.1j)
    assert a.isclose(b, rel_tol=1e-12, abs_tol=0.2)
    assert not a.isclose(b, rel_tol=1e-12, abs_tol=0.05)
    assert b.isclose(a, rel_tol=1e-12, abs_tol=0.2)
    assert not b.isclose(a, rel_tol=1e-12, abs_tol=0.05)
Exemplo n.º 5
0
def test_compress():
    a = qo.QubitOperator('X0', .9e-12)
    assert len(a.terms) == 1
    a.compress()
    assert len(a.terms) == 0
    a = qo.QubitOperator('X0', 1. + 1j)
    a.compress(.5)
    assert len(a.terms) == 1
    for term in a.terms:
        assert a.terms[term] == 1. + 1j
    a = qo.QubitOperator('X0', 1.1 + 1j)
    a.compress(1.)
    assert len(a.terms) == 1
    for term in a.terms:
        assert a.terms[term] == 1.1
    a = qo.QubitOperator('X0', 1.1 + 1j) + qo.QubitOperator('X1', 1.e-6j)
    a.compress()
    assert len(a.terms) == 2
    for term in a.terms:
        assert isinstance(a.terms[term], complex)
    a.compress(1.e-5)
    assert len(a.terms) == 1
    for term in a.terms:
        assert isinstance(a.terms[term], complex)
    a.compress(1.)
    assert len(a.terms) == 1
    for term in a.terms:
        assert isinstance(a.terms[term], float)
Exemplo n.º 6
0
def test_neg():
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    -op
    # out of place
    assert op.isclose(qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5))
    correct = -1.0 * op
    assert correct.isclose(-op)
Exemplo n.º 7
0
def test_str_multiple_terms():
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op += qo.QubitOperator(((1, 'Y'), (3, 'Y'), (8, 'Z')), 0.6)
    assert (str(op) == "0.5 X1 Y3 Z8 +\n0.6 Y1 Y3 Z8"
            or str(op) == "0.6 Y1 Y3 Z8 +\n0.5 X1 Y3 Z8")
    op2 = qo.QubitOperator((), 2)
    assert str(op2) == "2 I"
Exemplo n.º 8
0
def test_imul_qubit_op():
    op1 = qo.QubitOperator(((0, 'Y'), (3, 'X'), (8, 'Z'), (11, 'X')), 3.j)
    op2 = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op1 *= op2
    correct_coefficient = 1.j * 3.0j * 0.5
    correct_term = ((0, 'Y'), (1, 'X'), (3, 'Z'), (11, 'X'))
    assert len(op1.terms) == 1
    assert correct_term in op1.terms
Exemplo n.º 9
0
def test_mul_multiple_terms():
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op += qo.QubitOperator(((1, 'Z'), (3, 'X'), (8, 'Z')), 1.2)
    op += qo.QubitOperator(((1, 'Z'), (3, 'Y'), (9, 'Z')), 1.4j)
    res = op * op
    correct = qo.QubitOperator((), 0.5**2 + 1.2**2 + 1.4j**2)
    correct += qo.QubitOperator(((1, 'Y'), (3, 'Z')), 2j * 1j * 0.5 * 1.2)
    assert res.isclose(correct)
Exemplo n.º 10
0
def test_imul_bidir():
    op_a = qo.QubitOperator(((1, 'Y'), (0, 'X')), -1j)
    op_b = qo.QubitOperator(((1, 'Y'), (0, 'X'), (2, 'Z')), -1.5)
    op_a *= op_b
    op_b *= op_a
    assert ((2, 'Z'), ) in op_a.terms
    assert op_a.terms[((2, 'Z'), )] == 1.5j
    assert ((0, 'X'), (1, 'Y')) in op_b.terms
    assert op_b.terms[((0, 'X'), (1, 'Y'))] == -2.25j
Exemplo n.º 11
0
def test_get_inverse():
    qo0 = qo.QubitOperator("X1 Z2", cmath.exp(0.6j))
    qo1 = qo.QubitOperator("", 1j)
    assert qo0.get_inverse().isclose(
        qo.QubitOperator("X1 Z2", cmath.exp(-0.6j)))
    assert qo1.get_inverse().isclose(qo.QubitOperator("", -1j))
    qo0 += qo1
    with pytest.raises(NotInvertible):
        qo0.get_inverse()
Exemplo n.º 12
0
def test_mul_out_of_place():
    op1 = qo.QubitOperator(((0, 'Y'), (3, 'X'), (8, 'Z'), (11, 'X')), 3.j)
    op2 = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op3 = op1 * op2
    correct_coefficient = 1.j * 3.0j * 0.5
    correct_term = ((0, 'Y'), (1, 'X'), (3, 'Z'), (11, 'X'))
    assert op1.isclose(
        qo.QubitOperator(((0, 'Y'), (3, 'X'), (8, 'Z'), (11, 'X')), 3.j))
    assert op2.isclose(qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5))
    assert op3.isclose(qo.QubitOperator(correct_term, correct_coefficient))
Exemplo n.º 13
0
def test_rescaling_of_indices():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend, engine_list=[])
    qureg = eng.allocate_qureg(4)
    eng.flush()
    op = qo.QubitOperator("X0 Y1 Z3", 1j)
    op | qureg
    eng.flush()
    assert saving_backend.received_commands[5].gate.isclose(
        qo.QubitOperator("X0 Y1 Z2", 1j))
    # test that gate creates a new QubitOperator
    assert op.isclose(qo.QubitOperator("X0 Y1 Z3", 1j))
Exemplo n.º 14
0
def test_add():
    term_a = ((1, 'X'), (3, 'Y'), (8, 'Z'))
    term_b = ((1, 'Z'), (3, 'Y'), (8, 'Z'))
    a = qo.QubitOperator(term_a, 1.0)
    b = qo.QubitOperator(term_b, 0.5)
    res = a + b + b
    assert len(res.terms) == 2
    assert res.terms[term_a] == pytest.approx(1.0)
    assert res.terms[term_b] == pytest.approx(1.0)
    # Test out of place
    assert a.isclose(qo.QubitOperator(term_a, 1.0))
    assert b.isclose(qo.QubitOperator(term_b, 0.5))
Exemplo n.º 15
0
def test_iadd_different_term():
    term_a = ((1, 'X'), (3, 'Y'), (8, 'Z'))
    term_b = ((1, 'Z'), (3, 'Y'), (8, 'Z'))
    a = qo.QubitOperator(term_a, 1.0)
    a += qo.QubitOperator(term_b, 0.5)
    assert len(a.terms) == 2
    assert a.terms[term_a] == pytest.approx(1.0)
    assert a.terms[term_b] == pytest.approx(0.5)
    a += qo.QubitOperator(term_b, 0.5)
    assert len(a.terms) == 2
    assert a.terms[term_a] == pytest.approx(1.0)
    assert a.terms[term_b] == pytest.approx(1.0)
Exemplo n.º 16
0
def test_get_merged():
    qo0 = qo.QubitOperator("X1 Z2", 1j)
    qo1 = qo.QubitOperator("Y3", 1j)
    assert qo0.isclose(qo.QubitOperator("X1 Z2", 1j))
    assert qo1.isclose(qo.QubitOperator("Y3", 1j))
    assert qo0.get_merged(qo1).isclose(qo.QubitOperator("X1 Z2 Y3", -1))
    with pytest.raises(NotMergeable):
        qo1.get_merged(T)
    qo2 = qo0 + qo1
    with pytest.raises(NotMergeable):
        qo2.get_merged(qo0)
    with pytest.raises(NotMergeable):
        qo0.get_merged(qo2)
Exemplo n.º 17
0
def test_sub():
    term_a = ((1, 'X'), (3, 'Y'), (8, 'Z'))
    term_b = ((1, 'Z'), (3, 'Y'), (8, 'Z'))
    a = qo.QubitOperator(term_a, 1.0)
    b = qo.QubitOperator(term_b, 0.5)
    res = a - b
    assert len(res.terms) == 2
    assert res.terms[term_a] == pytest.approx(1.0)
    assert res.terms[term_b] == pytest.approx(-0.5)
    res2 = b - a
    assert len(res2.terms) == 2
    assert res2.terms[term_a] == pytest.approx(-1.0)
    assert res2.terms[term_b] == pytest.approx(0.5)
Exemplo n.º 18
0
def test_wrong_input():
    eng = MainEngine()
    qureg = eng.allocate_qureg(3)
    op0 = qo.QubitOperator("X1", 0.99)
    with pytest.raises(TypeError):
        op0 | qureg
    op1 = qo.QubitOperator("X2", 1)
    with pytest.raises(ValueError):
        op1 | qureg[1]
    with pytest.raises(TypeError):
        op0 | (qureg[1], qureg[2])
    op2 = op0 + op1
    with pytest.raises(TypeError):
        op2 | qureg
Exemplo n.º 19
0
def test_isub_different_term():
    term_a = ((1, 'X'), (3, 'Y'), (8, 'Z'))
    term_b = ((1, 'Z'), (3, 'Y'), (8, 'Z'))
    a = qo.QubitOperator(term_a, 1.0)
    a -= qo.QubitOperator(term_b, 0.5)
    assert len(a.terms) == 2
    assert a.terms[term_a] == pytest.approx(1.0)
    assert a.terms[term_b] == pytest.approx(-0.5)
    a -= qo.QubitOperator(term_b, 0.5)
    assert len(a.terms) == 2
    assert a.terms[term_a] == pytest.approx(1.0)
    assert a.terms[term_b] == pytest.approx(-1.0)
    b = qo.QubitOperator(term_a, 1.0)
    b -= qo.QubitOperator(term_a, 1.0)
    assert b.terms == {}
Exemplo n.º 20
0
def test_itruediv_and_idiv(divisor):
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    original = copy.deepcopy(op)
    correct = op * (1.0 / divisor)
    op /= divisor
    assert op.isclose(correct)
    # Test if done in-place
    assert not op.isclose(original)
Exemplo n.º 21
0
def test_truediv_and_div(divisor):
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    original = copy.deepcopy(op)
    res = op / divisor
    correct = op * (1.0 / divisor)
    assert res.isclose(correct)
    # Test if done out of place
    assert op.isclose(original)
Exemplo n.º 22
0
def test_itruediv_and_idiv(divisor):
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op2 = copy.deepcopy(op)
    original = copy.deepcopy(op)
    correct = op * (1. / divisor)
    op /= divisor
    op2.__idiv__(divisor)  # To test python 2 version as well
    assert op.isclose(correct)
    assert op2.isclose(correct)
    # Test if done in-place
    assert not op.isclose(original)
    assert not op2.isclose(original)
Exemplo n.º 23
0
def test_truediv_and_div(divisor):
    op = qo.QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5)
    op2 = copy.deepcopy(op)
    original = copy.deepcopy(op)
    res = op / divisor
    res2 = op2.__div__(divisor)  # To test python 2 version as well
    correct = op * (1. / divisor)
    assert res.isclose(correct)
    assert res2.isclose(correct)
    # Test if done out of place
    assert op.isclose(original)
    assert op2.isclose(original)
Exemplo n.º 24
0
def test_isclose_abs_tol():
    a = qo.QubitOperator('X0', -1.)
    b = qo.QubitOperator('X0', -1.05)
    c = qo.QubitOperator('X0', -1.11)
    assert a.isclose(b, rel_tol=1e-14, abs_tol=0.1)
    assert not a.isclose(c, rel_tol=1e-14, abs_tol=0.1)
    a = qo.QubitOperator('X0', -1.0j)
    b = qo.QubitOperator('X0', -1.05j)
    c = qo.QubitOperator('X0', -1.11j)
    assert a.isclose(b, rel_tol=1e-14, abs_tol=0.1)
    assert not a.isclose(c, rel_tol=1e-14, abs_tol=0.1)
Exemplo n.º 25
0
def test_init_bad_action_in_tuple():
    with pytest.raises(ValueError):
        qubit_op = qo.QubitOperator(((1, 'Q'), ))
Exemplo n.º 26
0
def test_init_bad_action():
    with pytest.raises(ValueError):
        qubit_op = qo.QubitOperator('Q0')
Exemplo n.º 27
0
def test_init_bad_coefficient():
    with pytest.raises(ValueError):
        qubit_op = qo.QubitOperator('X0', "0.5")
Exemplo n.º 28
0
def test_init_bad_term():
    with pytest.raises(ValueError):
        qubit_op = qo.QubitOperator(list())
Exemplo n.º 29
0
def test_init_str_identity():
    qubit_op = qo.QubitOperator('', 2.)
    assert len(qubit_op.terms) == 1
    assert () in qubit_op.terms
    assert qubit_op.terms[()] == pytest.approx(2.)
Exemplo n.º 30
0
def test_init_str():
    qubit_op = qo.QubitOperator('X0 Y5 Z12', -1.)
    correct = ((0, 'X'), (5, 'Y'), (12, 'Z'))
    assert correct in qubit_op.terms
    assert qubit_op.terms[correct] == -1.0