def __call__(self, p, *args, **kwargs): """ call a wrapped objective. Parameters ---------- p: numpy array: Parameters with which to call the objective. args kwargs Returns ------- numpy.array: value of self.objective with p translated into variables, as a numpy array. """ angles = dict((self.param_keys[i], p[i]) for i in range(self.N)) if self.passive_angles is not None: angles = {**angles, **self.passive_angles} vars = format_variable_dictionary(angles) E = self.objective(variables=vars, samples=self.samples) if self.print_level > 2: print("E={:+2.8f}".format(E), " angles=", angles, " samples=", self.samples) elif self.print_level > 1: print("E={:+2.8f}".format(E)) if self.save_history: self.history.append(E) self.history_angles.append(angles) return numpy.float64(E) # jax types confuses optimizers
def simulate(objective: typing.Union['Objective', 'QCircuit'], variables: Dict[Union[Variable, Hashable], RealNumber] = None, samples: int = None, backend: str = None, noise: NoiseModel = None, device: str = None, *args, **kwargs) -> Union[RealNumber, 'QubitWaveFunction']: """Simulate a tequila objective or circuit Parameters ---------- objective: Objective: tequila objective or circuit variables: Dict: The variables of the objective given as dictionary with keys as tequila Variables/hashable types and values the corresponding real numbers samples : int, optional: if None a full wavefunction simulation is performed, otherwise a fixed number of samples is simulated backend : str, optional: specify the backend or give None for automatic assignment noise: NoiseModel, optional: specify a noise model to apply to simulation/sampling device: a device upon which (or in emulation of which) to sample *args : **kwargs : read_out_qubits = list[int] (define the qubits which shall be measured, has only effect on pure QCircuit simulation with samples) Returns ------- float or QubitWaveFunction the result of simulation. """ variables = format_variable_dictionary(variables) if variables is None and not (len(objective.extract_variables()) == 0): raise TequilaException( "You called simulate for a parametrized type but forgot to pass down the variables: {}" .format(objective.extract_variables())) compiled_objective = compile(objective=objective, samples=samples, variables=variables, backend=backend, noise=noise, device=device, *args, **kwargs) return compiled_objective(variables=variables, samples=samples, *args, **kwargs)
def simulate(objective: typing.Union['Objective', 'QCircuit'], variables: Dict[Union[Variable, Hashable], RealNumber] = None, samples: int = None, backend: str = None, noise_model=None, *args, **kwargs) -> Union[RealNumber, 'QubitWaveFunction']: """Simulate a tequila objective or circuit Parameters ---------- objective : tequila objective or circuit variables : The variables of the objective given as dictionary with keys as tequila Variables/hashable types and values the corresponding real numbers samples : int : (Default value = None) if None a full wavefunction simulation is performed, otherwise a fixed number of samples is simulated backend : str : (Default value = None) specify the backend or give None for automatic assignment noise_model: NoiseModel : specify a noise model to apply to simulation/sampling *args : **kwargs : Returns ------- type simulated/sampled objective or simulated/sampled wavefunction """ variables = format_variable_dictionary(variables) if variables is None and not (len(objective.extract_variables()) == 0): raise TequilaException( "You called simulate for a parametrized type but forgot to pass down the variables: {}" .format(objective.extract_variables())) compiled_objective = compile(objective=objective, samples=samples, variables=variables, backend=backend, noise_model=noise_model, *args, **kwargs) return compiled_objective(variables=variables, samples=samples, *args, **kwargs)
def draw(objective, variables=None, backend: str = None): """ Pretty output (depends on installed backends) Parameters ---------- objective : the tequila objective to print out variables : optional: Give variables if the objective is parametrized (not necesarry for displaying) backend: str, optional: chose preferred backend (of None or not found it will be automatically picked) """ if backend not in INSTALLED_SIMULATORS: backend = None if backend is None: if "cirq" in INSTALLED_SIMULATORS: backend = "cirq" elif "qiskit" in INSTALLED_SIMULATORS: backend = "qiskit" if isinstance(objective, Objective) or isinstance(objective, VectorObjective): print(objective) drawn = {} for i, E in enumerate(objective.get_expectationvalues()): if E in drawn: print("\nExpectation Value {} is the same as {}".format( i, drawn[E])) else: print("\nExpectation Value {}".format(i)) print("Hamiltonian : ", E.H) print("variables : ", E.U.extract_variables()) print("circuit:\n") draw(E.U, backend=backend) drawn[E] = i else: if backend is None: print(objective) else: if variables is None: variables = {} for k in objective.extract_variables(): if k not in variables: variables[k] = 0.0 variables = format_variable_dictionary(variables) compiled = compile_circuit(abstract_circuit=objective, backend=backend, variables=variables) print(compiled.circuit)
def __call__(self, p, *args, **kwargs): angles = dict((self.param_keys[i], p[i]) for i in range(self.N)) if self.passive_angles is not None: angles = {**angles, **self.passive_angles} vars = format_variable_dictionary(angles) E = self.objective(variables=vars, samples=self.samples, **self.backend_options) if not self.silent: print("E=", E, " angles=", angles, " samples=", self.samples) if self.save_history: self.history.append(E) self.history_angles.append(angles) return numpy.float64(E) # jax types confuses optimizers