def test_to_operator(): bs0 = BaseQubitState("0") s0 = bs0.to_state() bop0 = BaseOperator(bs0, bs0) print(bop0.to_operator()) print(Operator([bop0])) assert bop0.to_operator() == Operator([bop0]) assert bop0.to_operator() == outer_product(s0, s0)
def test_fock_base_op(): bsaw = BaseFockState([FockOp("a", "w")]) bsav = BaseFockState([FockOp("a", "v")]) bopaw = BaseOperator(bsaw, bsaw) bopav = BaseOperator(bsav, bsav) assert get_variables(bopaw) == set(["w"]) assert get_variables(bopav) == set(["v"]) new = replace_var(bopaw, "w", "v") assert bopav == new
def test_operator_variables(): bsaw = BaseFockState([FockOp("a", "w")]) bsav = BaseFockState([FockOp("a", "v")]) opaw = BaseOperator(bsaw, bsaw).to_operator() opav = BaseOperator(bsav, bsav).to_operator() assert get_variables(opaw) == set(["w"]) assert get_variables(opav) == set(["v"]) new = replace_var(opaw, "w", "v") assert opav == new
def test_simplify_operator(): bs0 = BaseQubitState("0") bs1 = BaseQubitState("1") op = Operator([BaseOperator(bs0, bs0)]) op._terms[BaseOperator(bs1, bs1)] = 0 assert len(op) == 2 op = simplify(op) assert len(op) == 1
def test_faulty_init_operator(): bs0 = BaseQubitState("0") bop0 = BaseOperator(bs0, bs0) with pytest.raises(ValueError): Operator([bop0], [1, 2]) # Wrong number of scalars with pytest.raises(TypeError): Operator([bs0], [1]) # Not a base operator with pytest.raises(TypeError): Operator([bop0], [bs0]) # Not a scalar bs00 = BaseQubitState("00") bop00 = BaseOperator(bs00, bs00) with pytest.raises(ValueError): Operator([bop0, bop00]) # Not add compatible
def test_non_complete_mul(): bs0 = BaseQubitState("0") s00 = BaseQubitState("00").to_state() bop = BaseOperator(bs0, bs0) with pytest.raises(TypeError): bop * s00 with pytest.raises(TypeError): bop * 1
def test_eq(): bs0 = BaseQubitState("0") bs1 = BaseQubitState("1") assert BaseOperator(bs0, bs0) == BaseOperator(bs0, bs0) assert BaseOperator(bs0, bs1) != BaseOperator(bs1, bs0) assert BaseOperator(bs0, bs0) != BaseOperator(bs1, bs1)
def test_mul_operator_scalar(): bs0 = BaseQubitState("0") bop0 = BaseOperator(bs0, bs0) op = Operator([bop0]) assert op * 0.5 == Operator([bop0], [0.5])
def test_faulty_init_base_operator(): with pytest.raises(TypeError): BaseOperator(None, None)