def test_hessian_based_methods(simulator, method, use_hessian): wfn = tq.QubitWaveFunction.from_string(string="1.0*|00> + 1.0*|11>") H = tq.paulis.Projector(wfn=wfn.normalize()) U = tq.gates.Ry(angle=tq.assign_variable("a") * numpy.pi, target=0) U += tq.gates.Ry(angle=tq.assign_variable("b") * numpy.pi, target=1, control=0) E = tq.ExpectationValue(H=H, U=U) method_options = {"gtol": 1.e-4} # need to improve starting points for some of the optimizations initial_values = {"a": 0.002, "b": 0.01} if method not in ["TRUST-CONSTR", "TRUST_KRYLOV]"]: method_options['eta'] = 0.1 method_options['initial_trust_radius'] = 0.1 method_options['max_trust_radius'] = 0.25 initial_values = {"a": 0.3, "b": 0.8} # numerical hessian only works for this method if use_hessian in ['2-point', '3-point']: if method != "TRUST-CONSTR": return initial_values = {"a": 0.3, "b": 0.8} result = tq.optimizer_scipy.minimize(objective=-E, backend=simulator, hessian=use_hessian, method=method, tol=1.e-4, method_options=method_options, initial_values=initial_values, silent=True) assert (numpy.isclose(result.energy, -1.0, atol=1.e-1))
def test_gradient_based_methods(simulator, method, use_gradient): wfn = tq.QubitWaveFunction.from_string(string="1.0*|00> + 1.0*|11>") H = tq.paulis.Projector(wfn=wfn.normalize()) U = tq.gates.Ry(angle=tq.assign_variable("a") * numpy.pi, target=0) U += tq.gates.Ry(angle=tq.assign_variable("b") * numpy.pi, target=1, control=0) E = tq.ExpectationValue(H=H, U=U) # need to improve starting points for some of the optimizations initial_values = {"a": 0.002, "b": 0.01} if method in ["L-BFGS-B", "TNC"]: initial_values = {"a": 0.1, "b": 0.8} if use_gradient is False: initial_values = {"a": 0.3, "b": 0.8} result = tq.optimizer_scipy.minimize(objective=-E, backend=simulator, gradient=use_gradient, method=method, tol=1.e-4, method_options={ "gtol": 1.e-4, "eps": 1.e-4 }, initial_values=initial_values, silent=True) assert (numpy.isclose(result.energy, -1.0, atol=1.e-1))
def test_gradient_based_methods(simulator, method, use_gradient): wfn = tq.QubitWaveFunction.from_string(string="1.0*|00> + 1.0*|11>") H = tq.paulis.Projector(wfn=wfn.normalize()) U = tq.gates.Ry(angle=tq.assign_variable("a") * numpy.pi, target=0) U += tq.gates.Ry(angle=tq.assign_variable("b") * numpy.pi, target=1, control=0) E = tq.ExpectationValue(H=H, U=U) initial_values = {"a": 3.45, "b": 2.85} # eps is absolute finite difference step of scipy (used only for gradient = False or scipy < 1.5) # finite_diff_rel_step is relative step result = tq.optimizer_scipy.minimize(objective=-E, backend=simulator, gradient=use_gradient, method=method, tol=1.e-3, method_options={ "gtol": 1.e-4, "eps": 1.e-4, "finite_diff_rel_step": 1.e-4 }, initial_values=initial_values, silent=True) assert (numpy.isclose(result.energy, -1.0, atol=1.e-1))
def test_moments(): c = QCircuit() c += CNOT(target=0, control=(1, 2, 3)) c += H(target=[0, 1]) c += Rx(angle=numpy.pi, target=[0, 3]) c += Z(target=1) c += Phase(phi=numpy.pi, target=4) moms = c.moments assert len(moms) == 3 assert (moms[0].gates[1].parameter == assign_variable(numpy.pi)) assert (moms[0].gates[1].target == (4, ))
def test_canonical_moments(): c = QCircuit() c += CNOT(target=0, control=(1, 2, 3)) c += Rx(angle=Variable('a'), target=[0, 3]) c += H(target=[0, 1]) c += Rx(angle=Variable('a'), target=[2, 3]) c += Rx(angle=Variable('a'), target=[0, 3]) c += Z(target=1) c += Phase(phi=numpy.pi, target=4) moms = c.canonical_moments assert len(moms) == 6 assert (moms[0].gates[1].parameter == assign_variable(numpy.pi)) assert (moms[0].gates[1].target == (4, )) assert hasattr(moms[3].gates[0], 'axis') assert len(moms[0].qubits) == 5
def manual_ansatz(n_qubits, n_layers=1, connectivity=None, static=False): # no pbc U = tq.QCircuit() for l in range(n_layers): for q in range(n_qubits): angle = (l,q,0) if not static else (l,0) angle = tq.assign_variable(angle) U += tq.gates.Ry(angle=angle*numpy.pi, target=q) for q in range(n_qubits//2): angle = (l, 2*q,2*q+1, 1) if not static else 1.0 angle = tq.assign_variable(angle) U += tq.gates.ExpPauli(paulistring="X({})Y({})".format(2*q,2*q+1), angle=angle*numpy.pi) #U += tq.gates.ExpPauli(paulistring="Y({})X({})".format(2*q,2*q+1), angle=angle*numpy.pi) # for q in range(n_qubits): # angle = (l, q,1) if not static else (l,1) # U += tq.gates.Ry(angle=angle, target=q) for q in range(n_qubits//2-1): angle = (l, 2*q+1, 2*q+2, 1) if not static else 1.0 angle = tq.assign_variable(angle) U += tq.gates.ExpPauli(paulistring="X({})Y({})".format(2*q+1,(2*q+2)%n_qubits), angle=angle*numpy.pi) #U += tq.gates.ExpPauli(paulistring="Y({})X({})".format(2*q+1,(2*q+2)%n_qubits), angle=angle*numpy.pi) return U