"""\********************************************************************************* Copyright (c) 2024 the Qrisp authors** This program and the accompanying materials are made available under the* terms of the Eclipse Public License 2.0 which is available at* http://www.eclipse.org/legal/epl-2.0.** This Source Code may also be made available under the following Secondary* Licenses when the conditions for such availability set forth in the Eclipse* Public License, v. 2.0 are satisfied: GNU General Public License, version 2* with the GNU Classpath Exception which is* available at https://www.gnu.org/software/classpath/license.html.** SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0********************************************************************************/"""fromqrispimportQuantumVariable,QuantumBool,x,mcxfromqrisp.algorithms.qaoa.mixersimportcontrolled_RX_mixer_gen
[docs]defcreate_min_set_cover_mixer(sets,universe):r""" Creates the ``controlled_RX_mixer`` for an instance of the minimum set cover problem following `Hadfield et al. <https://arxiv.org/abs/1709.03489>`_ The belonging ``predicate`` function indicates if a set can be swapped out of the solution. Parameters ---------- sets : list[set] A list of sets. universe : set The union of all sets. Returns ------- function A Python function receiving a :ref:`QuantumVariable` and real parameter $\beta$. This function performs the application of the mixer associated to the problem instance. """membership_dict={element:[ifori,subsetinenumerate(sets)ifelementinsubset]forelementinuniverse}defpredicate(qv,i):anc=QuantumVariable(len(sets[i]))x(anc)foranc_index,elementinenumerate(sets[i]):other_sets=[itemforiteminmembership_dict[element]ifitem!=i]mcx([qv[set_index]forset_indexinother_sets],anc[anc_index],ctrl_state="0"*len(other_sets))qbl=QuantumBool()mcx(anc,qbl)returnqblcontrolled_RX_mixer=controlled_RX_mixer_gen(predicate)returncontrolled_RX_mixer
[docs]defcreate_min_set_cover_cl_cost_function(sets,universe):""" Creates the classical cost function for an instance of the minimum set cover problem. Parameters ---------- sets : list[set] A list of sets. universe : set The union of all sets. Returns ------- cl_cost_function : function The classical cost function for the problem instance, which takes a dictionary of measurement results as input. """defcl_cost_function(res_dic):cost=0forstate,probinres_dic.items():indices=[indexforindex,valueinenumerate(state)ifvalue=='1']solution_sets=[sets[index]forindexinindices]iflen(solution_sets)>0andset.union(*solution_sets)==universe:cost+=len(indices)*probelse:cost+=len(sets)returncostreturncl_cost_function
[docs]defmin_set_cover_init_function(qv):r""" Prepares the initial state $\ket{1}^{\otimes n}$. Parameters ---------- qv : :ref:`QuantumVariable` The quantum argument. """x(qv)
[docs]defmin_set_cover_problem(sets,universe):""" Creates a QAOA problem instance with appropriate phase separator, mixer, and classical cost function. Parameters ---------- sets : list[set] A list of sets. universe : set The union of all sets. Returns ------- :ref:`QAOAProblem` A QAOA problem instance for MinSetCover for given ``sets`` and ``universe``. """fromqrisp.qaoaimportQAOAProblem,RZ_mixerreturnQAOAProblem(cost_operator=RZ_mixer,mixer=create_min_set_cover_mixer(sets,universe),cl_cost_function=create_min_set_cover_cl_cost_function(sets,universe),init_function=min_set_cover_init_function)
Get in touch!
If you are interested in Qrisp or high-level quantum algorithm research in general connect with us on our
Slack workspace.