def test_gradient_based_methods_qng(simulator, method): H = tq.paulis.Y(0) U = tq.gates.Ry(numpy.pi / 4, 0) + tq.gates.Ry( numpy.pi / 3, 1) + tq.gates.Ry(numpy.pi / 7, 2) U += tq.gates.Rz('a', 0) + tq.gates.Rz('b', 1) U += tq.gates.CNOT(control=0, target=1) + tq.gates.CNOT(control=1, target=2) U += tq.gates.Ry('c', 1) + tq.gates.Rx('d', 2) U += tq.gates.CNOT(control=0, target=1) + tq.gates.CNOT(control=1, target=2) E = tq.ExpectationValue(H=H, U=U) # just equal to the original circuit, but i'm checking that all the sub-division works O = (4 / 8) * E + (3 / 8) * copy.deepcopy(E) + ( 1 / 8) * copy.deepcopy(E) + tq.Variable('a') - tq.Variable('a') initial_values = {"a": 0.432, "b": -0.123, 'c': 0.543, 'd': 0.233} result = tq.optimizer_scipy.minimize(objective=-O, qng=True, backend=simulator, method=method, tol=1.e-4, method_options={ "gtol": 1.e-4, "eps": 1.e-4 }, initial_values=initial_values, silent=False) assert (numpy.isclose(result.energy, -0.612, atol=1.e-1))
def test_parametrized_multi_pauli_rotation(simulator, angle, ps): a = tq.Variable("a") variables = {a: angle} U = tq.gates.X(target=1) + tq.gates.X(target=0, control=1) + tq.gates.ExpPauli(angle=a, paulistring=ps) wfn1 = tequila.simulators.simulator_api.simulate(U, variables, backend=None) wfn2 = tequila.simulators.simulator_api.simulate(U, variables, backend=simulator) assert (numpy.isclose(numpy.abs(wfn1.inner(wfn2)) ** 2, 1.0, atol=1.e-4))
def test_bit_flip_phoenics(simulator, p): qubit = 0 H = paulis.Qm(qubit) U = gates.Rx(target=qubit, angle=tq.Variable('a')) O = ExpectationValue(U=U, H=H) NM = BitFlip(p, 1) result = tq.optimizers.optimizer_phoenics.minimize(objective=O, maxiter=3, samples=1, backend=simulator, noise=NM)
def ans4(num_qubits, layers, arg, enc): U = tq.QCircuit() for l in range(layers): # Layer single-qubit gates for q in range(num_qubits): # No encoding: if enc == False: theta = tq.Variable(name="th_{}{}".format(l, q)) phi = tq.Variable(name="ph_{}{}".format(l, q)) U += tq.gates.Rx(target=q, angle=theta) + tq.gates.Rz(target=q, angle=phi) # Encoding: if enc == True: theta = tq.Variable(name="thenc_{}{}".format(l, q)) phi = tq.Variable(name="phenc_{}{}".format(l, q)) wth = tq.Variable(name="thw_{}{}".format(l, q)) wph = tq.Variable(name="phw_{}{}".format(l, q)) U += tq.gates.Rx(target=q, angle=wth*arg + theta) + tq.gates.Rz(target=q, angle=wph*arg + phi) # Layer XX gates # no encoding in entangling gates # SAME entangling gates U += XX(0,1,"a") + XX(2,3,"b") + XX(1,2,"c") + XX(0,3,"d") + XX(0,2,"e") + XX(1,3,"f") #if enc == False: #U += XX(0,1, "a{}".format(l)) + XX(2,3,"b{}".format(l)) + XX(1,2,"c{}".format(l)) + XX(0,3,"d{}".format(l)) + XX(0,2,"e{}".format(l)) + XX(1,3,"f{}".format(l)) #if enc == True: # U += XX(0,1, "aw{}".format(l)) + XX(2,3,"bw{}".format(l)) + XX(1,2,"cw{}".format(l)) + XX(0,3,"dw{}".format(l)) + XX(0,2,"ew{}".format(l)) + XX(1,3,"fw{}".format(l)) return (U)
def test_bit_flip_scipy_hessian(simulator, p, method): qubit = 0 H = paulis.Qm(qubit) U = gates.Rx(target=qubit, angle=tq.Variable('a')) O = ExpectationValue(U=U, H=H) NM = BitFlip(p, 1) result = tq.optimizer_scipy.minimize(objective=O, samples=1, backend=simulator, method=method, noise=NM, tol=1.e-4, silent=False)
def test_gradient_based_methods_qng(simulator, method): H = tq.paulis.Y(0) U = tq.gates.Ry(numpy.pi / 4, 0) + tq.gates.Ry( numpy.pi / 3, 1) + tq.gates.Ry(numpy.pi / 7, 2) U += tq.gates.Rz('a', 0) + tq.gates.Rz('b', 1) U += tq.gates.CNOT(control=0, target=1) + tq.gates.CNOT(control=1, target=2) U += tq.gates.Ry('c', 1) + tq.gates.Rx('d', 2) U += tq.gates.CNOT(control=0, target=1) + tq.gates.CNOT(control=1, target=2) E = tq.ExpectationValue(H=H, U=U) # just equal to the original circuit, but i'm checking that all the sub-division works O = (4 / 8) * E + (3 / 8) * copy.deepcopy(E) + ( 1 / 8) * copy.deepcopy(E) + tq.Variable('a') - tq.Variable('a') if method == 'CG' or method == 'TNC': ### these methods SUCK. these parameters are almost correct, drawn from the history of more successful opt. initial_values = { 'b': 1.2015702944309066, 'a': 0.36870888682982483, 'c': 1.4772702039340424, 'd': -0.5109743059462741 } else: initial_values = {"a": 0.432, "b": -0.123, 'c': 0.543, 'd': 0.233} result = tq.optimizer_scipy.minimize(objective=-O, qng=True, backend=simulator, method=method, tol=1.e-4, method_options={ "gtol": 1.e-4, "eps": 1.e-4 }, initial_values=initial_values, silent=False) assert (numpy.isclose(result.energy, -0.612, atol=1.e-1))
from photonic import PhotonicSetup import tequila as tq import pickle """ Set The Parameters for the encoding here: """ # If you want to change S and qpm you need to think about new encodings for the P1 projector S = 1 # Can not be changed for this example. Hamiltonians are wrong otherwise qpm = 1 # Can not be changed for this example. Hamiltonians are wrong otherwise trotter_steps = 1 # number of trotter steps for the BeamSplitter (for S=1 and qpm=1, one trotter step is fine) if __name__ == "__main__": param_dove = tq.Variable( name="t" ) # the dove prism is already defined in units of pi within PhotonicSetup angle0 = tq.numpy.pi * tq.Variable(name="angle0") # angles in units of pi angle1 = tq.numpy.pi * tq.Variable(name="angle1") # angles in units of pi # Use the PhotonicSetup helper class to initialize the mapped parametrized photonic setup setup = PhotonicSetup(pathnames=['a', 'b', 'c', 'd'], S=S, qpm=qpm) setup.prepare_SPDC_state(path_a='a', path_b='b') setup.prepare_SPDC_state(path_a='c', path_b='d') setup.add_beamsplitter(path_a='b', path_b='c', t=0.25, steps=trotter_steps) setup.add_doveprism(path='c', t=param_dove) setup.add_beamsplitter(path_a='b', path_b='c', t=0.25, steps=trotter_steps) setup.add_parametrized_one_photon_projector(path='a', angles=[angle0, angle1]) # this is the mapped photonic setup U_pre = setup.setup
def XX(q0,q1,angle): return(tq.gates.ExpPauli(paulistring="X({})X({})".format(q0,q1), angle=tq.Variable(name=angle)))
# Generate the training points if equispaced_train == True: arg_train = [arg_min + i*(arg_max-arg_min)/(n_train-1) for i in range(n_train)] else: arg_train = [random.uniform(arg_min, arg_max) for i in range(n_train)] arg_train.sort() Obj = tq.Objective() for i in range(n_train): Ham = Htrans(arg_train[i]) total_U = ans4(n_qub, n_lay[0], arg_train[i], True) + ans4(n_qub, n_lay[1], arg_train[i], False) if original == False: # last layer of single-qubit gates for q in range(n_qub): theta = tq.Variable(name="th2_{}".format(q)) phi = tq.Variable(name="ph2_{}".format(q)) # No encoding: total_U += tq.gates.Rx(target=q, angle=theta) + tq.gates.Rz(target=q, angle=phi) Obj += tq.ExpectationValue(H=Ham, U=total_U) # Number of optimization variables print("Meta-VQE training: ", len(Obj.extract_variables()), file=file_data_varcount) variables = Obj.extract_variables() variables = sorted(variables, key=lambda x: x.name) # Random initialization of variables th0 = {key: random.uniform(0, np.pi) for key in variables}