def test_gradient_UY_HX_wfnsim(simulator, angle_value, controlled, assume_real, silent=True): # same as before just with wavefunction simulation # case X Y # U = cos(angle/2) + sin(-angle/2)*i*Y # <0|Ud H U |0> = cos^2(angle/2)*<0|X|0> # + sin^2(-angle/2) <0|YXY|0> # + cos(angle/2)*sin(angle/2)*i<0|XY|0> # + sin(-angle/2)*cos(angle/2)*(-i) <0|YX|0> # = cos^2*0 + sin^2*0 + cos*sin*i(<0|[XY,YX]|0>) # = 0.5*sin(-angle)*i <0|[XY,YX]|0> = -0.5*sin(angle)*i * 2 i <0|Z|0> # = sin(angle) angle = Variable(name="angle") variables = {angle: angle_value} qubit = 0 H = paulis.X(qubit=qubit) if controlled: control = 1 U = gates.X(target=control) + gates.Ry(target=qubit, control=control, assume_real=assume_real, angle=angle) else: U = gates.Ry(target=qubit, angle=angle) O = ExpectationValue(U=U, H=H) E = simulate(O, variables=variables, backend=simulator) dO = grad(objective=O, variable='angle') dE = simulate(dO, variables=variables, backend=simulator) E = numpy.float(E) # for isclose dE = numpy.float(dE) # for isclose assert (numpy.isclose(E, numpy.sin(angle(variables)), atol=0.0001)) assert (numpy.isclose(dE, numpy.cos(angle(variables)), atol=0.0001)) if not silent: print("E =", E) print("sin(angle)=", numpy.sin(angle(variables))) print("dE =", dE) print("cos(angle)=", numpy.cos(angle(variables)))
def test_r_power(simulator, value=numpy.random.uniform(0.0, 2.0 * numpy.pi, 1)[0]): angle1 = Variable(name="angle1") variables = {angle1: value} qubit = 0 control = 1 H1 = paulis.X(qubit=qubit) U1 = gates.X(target=control) + gates.Ry( target=qubit, control=control, angle=angle1) e1 = ExpectationValue(U=U1, H=H1) added = 2**e1 val = simulate(added, variables=variables, backend=simulator) en1 = 2**simulate(e1, variables=variables, backend=simulator) an1 = 2.**np.sin(angle1(variables=variables)) assert np.isclose(val, en1, atol=1.e-4) assert np.isclose(val, an1, atol=1.e-4)
def test_l_addition(simulator, value=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))): angle1 = Variable(name="angle1") variables = {angle1: value} qubit = 0 control = 1 H1 = paulis.X(qubit=qubit) U1 = gates.X(target=control) + gates.Ry( target=qubit, control=control, angle=angle1) e1 = ExpectationValue(U=U1, H=H1) added = e1 + 1 val = simulate(added, variables=variables, backend=simulator) en1 = simulate(e1, variables=variables, backend=simulator) + 1. an1 = np.sin(angle1(variables=variables)) + 1. assert np.isclose(val, en1, atol=1.e-4) assert np.isclose(val, an1, atol=1.e-4)
def test_heterogeneous_operations_l(simulator, op, value1=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0)), value2=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0))): angle1 = Variable(name="angle1") angle2 = Variable(name="angle2") variables = {angle1: value1, angle2: value2} qubit = 0 control = 1 H2 = paulis.X(qubit=qubit) U2 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle2) e2 = ExpectationValue(U=U2, H=H2) added = Objective(args=[angle1, e2.args[0]], transformation=op) val = simulate(added, variables=variables, backend=simulator) en2 = simulate(e2, variables=variables, backend=simulator) an1 = angle1(variables=variables) an2 = np.sin(angle2(variables=variables)) assert np.isclose(val, float(op(an1, en2)), atol=1.e-4) assert np.isclose(en2, an2, atol=1.e-4)
def test_mixed_power(simulator, value1=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0)), value2=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0))): angle1 = Variable(name="angle1") angle2 = Variable(name="angle2") variables = {angle1: value1, angle2: value2} qubit = 0 control = 1 H1 = paulis.X(qubit=qubit) U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1) e1 = ExpectationValue(U=U1, H=H1) H2 = paulis.Y(qubit=qubit) U2 = gates.X(target=control) + gates.Rx(target=qubit, control=control, angle=angle2) e2 = ExpectationValue(U=U2, H=H2) added = (e1+1.e-4) ** e2 val = simulate(added, variables=variables, backend=simulator) en1 = simulate(e1, variables=variables, backend=simulator) en2 = simulate(e2, variables=variables, backend=simulator) an1 = np.sin(angle1(variables=variables)) an2 = -np.sin(angle2(variables=variables)) assert np.isclose(val, (en1+1.e-4) ** en2, atol=1.e-4) assert np.isclose(val, (an1+1.e-4) ** an2, atol=1.e-4)
def test_compilation(backend): U = gates.X(target=[0,1,2,3,4,5]) for i in range(10): U += gates.Ry(angle=(i,), target=numpy.random.randint(1,5,1)[0]) U += gates.CZ(0,1) + gates.CNOT(1,2) + gates.CZ(2,3) + gates.CNOT(3,4) + gates.CZ(5,6) H = paulis.X(0) + paulis.X(1) + paulis.X(2) + paulis.X(3) + paulis.X(4) + paulis.X(5) H += paulis.Z(0) + paulis.Z(1) + paulis.Z(2) + paulis.Z(3) + paulis.Z(4) + paulis.Z(5) E = ExpectationValue(H=H, U=U) randvals = numpy.random.uniform(1.0, 1.9, 10) variables = {(i,): randvals[i] for i in range(10)} e0 = simulate(E, variables=variables, backend=backend) E2 = E*E for i in range(99): E2 += E*E compiled = tq.compile(E2, variables=variables, backend=backend) e2 = compiled(variables=variables) assert(E2.count_expectationvalues(unique=True) == 1) assert(compiled.count_expectationvalues(unique=True) == 1) assert numpy.isclose(100*e0**2, e2)
with open(filename + ".tmp.tex", "w") as file: file.write(latex_header + "\n") file.write("\\begin{document}\n") file.write("\\input{" + filename + "}\n") file.write("\\end{document}\n") with open(filename + "tmp.log", "w") as file: subprocess.call(["pdflatex", str(filename) + ".tmp.tex"], stdout=file) move(filename + ".tmp.pdf", filename + ".pdf") remove(filename + ".tmp.aux") remove(filename + ".tmp.log") remove(filename + ".tmp.tex") if not keep_qpic: remove(filename + ".qpic") if not keep_tex: remove(filename + ".tex") if __name__ == "__main__": circuit = gates.X(0) + gates.H(1) + gates.X(target=0, control=1) + gates.Ry(target=0, control=1, angle="\\theta") + gates.X(target=2, control=[0, 1]) string = export_to_qpic(circuit) print(string) export_to_pdf(circuit, filename="test", keep_qpic=True, keep_tex=True)