import proveit
# Automation is not needed when building an expression:
proveit.defaults.automation = False # This will speed things up.
proveit.defaults.inline_pngs = False # Makes files smaller.
%load_expr # Load the stored expression as 'stored_expr'
# import Expression classes needed to build the expression
from proveit import ExprRange, U, Variable, VertExprArray, m
from proveit.linear_algebra import ScalarMult, VecAdd
from proveit.logic import Set
from proveit.numbers import Add, Exp, Interval, Mult, e, frac, i, one, pi, sqrt, two
from proveit.physics.quantum import CONTROL, ket0, ket1, ket_plus, var_ket_u, varphi
from proveit.physics.quantum.circuits import Gate, Input, MultiQubitElem, Output, Qcircuit
# build up the expression from sub-expressions
sub_expr1 = Variable("_a", latex_format = r"{_{-}a}")
sub_expr2 = Add(m, one)
sub_expr3 = Interval(two, Add(m, two))
expr = Qcircuit(vert_expr_array = VertExprArray([Input(state = ket_plus), ExprRange(sub_expr1, MultiQubitElem(element = Input(state = var_ket_u, part = sub_expr1), targets = sub_expr3), one, sub_expr2)], [MultiQubitElem(element = CONTROL, targets = Set(two)), ExprRange(sub_expr1, MultiQubitElem(element = Gate(operation = U, part = sub_expr1), targets = sub_expr3), one, sub_expr2)], [Output(state = ScalarMult(frac(one, sqrt(two)), VecAdd(ket0, ScalarMult(Exp(e, Mult(two, pi, i, varphi)), ket1)))), ExprRange(sub_expr1, MultiQubitElem(element = Output(state = var_ket_u, part = sub_expr1), targets = sub_expr3), one, sub_expr2)]))
# check that the built expression is the same as the stored expression
assert expr == stored_expr
assert expr._style_id == stored_expr._style_id
print("Passed sanity check: expr matches stored_expr")
# Show the LaTeX representation of the expression for convenience if you need it.
print(stored_expr.latex())
stored_expr.style_options()
# display the expression information
stored_expr.expr_info()