Package: ewl
EWLA simple Python library to simulate and execute EWL quantum circuits on IBM Q with symbolic calculations using SymPy.InstallationpipinstallewlExamplesPrisoner's dilemmaTwo playersThree playersPayoff function 3D plotSimulation with predefined gate noisesUsageInitializationThis library usesSymPyto perform symbolic calculations. It's convenient to import it asspand define frequently used constants for future use.importsympyasspi=sp.Ipi=sp.pisqrt2=sp.sqrt(2)When using this library in Jupyter Notebook, callinit_printingto enable pretty printing.sp.init_printing()EWL instanceFirst, you need to define the initial (preferably entangled) quantum state:fromsympy.physics.quantum.qubitimportQubitpsi=(Qubit('00')+i*Qubit('11'))/sqrt2It is also necessary to define two unitary strategies that represent the classical strategies:C=sp.Matrix([[1,0],[0,1],])D=sp.Matrix([[0,i],[i,0],])Then you need to define the players' strategies. Each strategy must be a unitary matrix as it represents a single-qubit quantum gate.alice=sp.Matrix([[1,0],[0,1],])The library comes with a series of built-in parametrizations, including the original one from EWL paper as well as other 2- and 3 degrees of freedom parametrizations (seehere).fromewl.parametrizationsimport*bob=U_Eisert_Wilkens_Lewenstein(theta=pi/2,phi=0)At this point you can also use arbitrary symbols and compound expressions to generalize the analysis.theta,gamma=sp.symbols('theta gamma',real=True)charlie=U_Eisert_Wilkens_Lewenstein(theta=theta,phi=gamma/2)You also need to define the payoff matrix, possibly with symbols, for arbitrary number of players.payoff_matrix=sp.Array([[[3,0],[5,1],],[[3,5],[0,1],],])Finally, you can make an instance of quantum game in the EWL protocol by providing the initial quantum state, a list of players' strategies and the payoff matrix with corresponding shape. The library supports arbitrary number of players, although it works best for 2-player games.fromewlimportEWLewl=EWL(psi=psi,C=C,D=D,players=[alice,bob],payoff_matrix=payoff_matrix)CalculationsBased on the provided initial quantum state, the library automatically calculates the corresponding matrix ofJandJ†gates.ewl.Jewl.J_HBased on the players' strategies, the library also calculates the amplitudes of the result quantum state in the computational basis.ewl.amplitudes()ewl.amplitudes(simplify=False)From the amplitudes one can easily derive the probabilities of possible game results. By default, the expressions are simplified using trigonometric identities. Make sure to enablereal=Trueflag when defining real-valued symbols to allow for further simplification.ewl.probs()ewl.probs(simplify=False)Finally, based on the payoff matrix and previously mentioned probabilities, the library calculates the payoff functions as symbolic expressions (possibly with parameters from the initial state and strategies).ewl.payoff_function(player=0)# first playerewl.payoff_function(player=1,simplify=False)# second playerewl.payoff_function(player=None)# payoff sumYou can also obtain a tuple containing the payoffs for each respective player.ewl.payoffs()ewl.payoffs(simplify=False)For quantum games parametrized with exactly two symbols, it is possible to plot a three-dimensional graph of the payoff function.fromewl.plottingimportplot_payoff_functionplot_payoff_function(ewl,player=0,x=alpha,x_min=0,x_max=pi/2,y=beta,y_min=0,y_max=pi/2)ParametersHere's how you can list all symbols used either in the initial quantum state or in the players' strategies:ewl.paramsYou can also substitute the symbols with specific values to obtain a non-parametrized instance of quantum game as new EWL instance:ewl_fixed=ewl.fix(theta=0,gamma=pi/2)It is also possible to substitute specific players' strategies for further analysis of a certain case of the game.ewl_CD=ewl.play(C,D)Mixed strategiesThe library also supports quantum games with mixed strategies. You can define a mixed strategy by passing a list of probabilities and corresponding pure strategies:fromewl.mixedimportMixedStrategyalice=MixedStrategy([(p1,U1),(p2,U2)])Note:The library will check if the probabilities sum up to 1. To disable this check, passcheck_sum=False.Then you can create an instance of EWL quantum game with mixed strategies:fromewl.mixedimportMixedEWLmixed_ewl=MixedEWL(psi=psi,C=C,D=D,players=[alice,bob],payoff_matrix=payoff_matrix)The major difference is that you cannot callamplitudesmethod due to the fact that the state of quantum game with mixed strategies cannot be expressed as a vector in a general case. Instead, you can calculate the density matrix.mixed_ewl.density_matrix()Other properties and methods such asJ,J_H,params,fix,probsandpayoff_functionbehave similarly as for the regular EWL instance with pure strategies.Example gamesThe library comes with a series of built-in example games, in particular a few variants of Quantum Prisoner's Dilemma with different parametrizations from various articles (seehere).Qiskit integrationThis library also integrates withQiskit, allowing arbitrary quantum games in the EWL protocol to be executed onIBM Qdevices. First, you need to load your credentials:fromqiskitimportIBMQIBMQ.load_account()When running locally, make sure to save the access token to disk first usingIBMQ.save_account.In order to access backend-specific features of EWL instance, first you need to convert it toEWL_IBMQinstance. Note that the input quantum game must be non-parametrized (cannot have any symbols).fromewl.ibmqimportEWL_IBMQewl_ibmq=EWL_IBMQ(ewl_fixed)You can also specify and apply noise model used in quantum simulation.fromqiskit.providers.aer.noiseimportNoiseModel,pauli_errorp_error=0.05bit_flip=pauli_error([('X',p_error),('I',1-p_error)])phase_flip=pauli_error([('Z',p_error),('I',1-p_error)])noise_model=NoiseModel()noise_model.add_all_qubit_quantum_error(bit_flip,['u1','u2','u3'])noise_model.add_all_qubit_quantum_error(phase_flip,['x'],[0])ewl_ibmq=EWL_IBMQ(ewl_fixed,noise_model=noise_model)You can draw the original quantum circuit of quantum game in the EWL protocol.ewl_ibmq.draw()It is also possible to draw the quantum circuit transpiled for a specific backend.ewl_ibmq.draw_transpiled(backend_name='ibmq_quito',optimization_level=3)Here's how you can execute the quantum game on a specific statevector simulator:ewl_ibmq.simulate_probs(backend_name='statevector_simulator')You may also run the quantum circuit on QASM simulator and get histogram data of the experiment.ewl_ibmq.simulate_counts(backend_name='qasm_simulator')Finally, you can run the quantum game on a real quantum device:ewl_ibmq.run(backend_name='ibmq_quito',optimization_level=3)Citation@software{PythonEWL2022,author={Tomasz Zawadzki and Piotr Kotara},title={A Python tool for symbolic analysis of quantum games in EWL protocol with IBM Q integration},howpublished={\url{https://github.com/tomekzaw/ewl}},}