def build_qaoa_ansatz_circuit( ansatz_specs: Specs, cost_hamiltonian: Union[str, List], mixer_hamiltonian: Union[str, List] = None, params: Optional[Union[str, List]] = None, ): if isinstance(ansatz_specs, str): DeprecationWarning( "Loading ansatz_specs as a string will be depreciated in future, please change it to a dictionary." ) ansatz_specs = json.loads(ansatz_specs) cost_hamiltonian = load_qubit_operator(cost_hamiltonian) if mixer_hamiltonian: mixer_hamiltonian = load_qubit_operator(mixer_hamiltonian) ansatz_specs["cost_hamiltonian"] = cost_hamiltonian ansatz_specs["mixer_hamiltonian"] = mixer_hamiltonian ansatz = load_from_specs(ansatz_specs) if params is not None: if isinstance(params, str): params = load_circuit_template_params(params) else: params = np.array(params) circuit = ansatz.get_executable_circuit(params) elif ansatz.supports_parametrized_circuits: circuit = ansatz.parametrized_circuit else: raise (Exception( "Ansatz is not parametrizable and no parameters has been provided." )) save_circuit(circuit, "circuit.json")
def build_ansatz_circuit(ansatz_specs: Specs, params: Optional[Union[str, List]] = None): ansatz = load_from_specs(ansatz_specs) if params is not None: if isinstance(params, str): params = load_circuit_template_params(params) else: params = np.array(params) circuit = ansatz.get_executable_circuit(params) elif ansatz.supports_parametrized_circuits: circuit = ansatz.parametrized_circuit else: raise (Exception( "Ansatz is not parametrizable and no parameters has been provided." )) save_circuit(circuit, "circuit.json")
def generate_random_ansatz_params( ansatz_specs: Optional[Specs] = None, number_of_parameters: Optional[int] = None, min_value: float = -np.pi * 0.5, max_value: float = np.pi * 0.5, seed: Optional[int] = None, ): assert (ansatz_specs is None) != (number_of_parameters is None) if ansatz_specs is not None: ansatz = load_from_specs(ansatz_specs) number_of_parameters = ansatz.number_of_params if seed is not None: np.random.seed(seed) params = np.random.uniform(min_value, max_value, number_of_parameters) serialization.save_array(params, "params.json")
def build_uniform_param_grid( ansatz_specs: Optional[Specs] = None, number_of_params_per_layer: Optional[int] = None, number_of_layers: int = 1, min_value: float = 0, max_value: float = 2 * np.pi, step: float = np.pi / 5, ): assert (ansatz_specs is None) != (number_of_params_per_layer is None) if ansatz_specs is not None: ansatz = load_from_specs(ansatz_specs) number_of_params = ansatz.number_of_params else: number_of_params = number_of_params_per_layer grid = _build_uniform_param_grid(number_of_params, number_of_layers, min_value, max_value, step) save_parameter_grid(grid, "parameter-grid.json")