JavaFXpert / llm-grovers-search-party

Leveraging Qiskit, GPT-3 and LangChain to demonstrate Grover's algorithm
Apache License 2.0
10 stars 1 forks source link

Help with search party #1

Open kozmando opened 10 months ago

kozmando commented 10 months ago

Hi, great idea and package. Had it running, but got OpenAI 429 rate limits. Then tried again today without luck. The warnings and errors are in bold. It seems there is a new langchain-openai package, but could be a fake package (https://github.com/langchain-ai/langchain/discussions/15839). We would like to help and contribute to this package, but we need some guidance on these current issues. Thanks.

Running python 3.10.11 on Windows 10 Running jupyter notebook

from qiskit_ibm_runtime import QiskitRuntimeService ibm_quantum_service = QiskitRuntimeService(channel="ibm_quantum", token="") import os os.environ["OPENAI_API_KEY"] = "" from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain llm = OpenAI(temperature=0.0) template = """Create a boolean expression that expresses the following situation: Situation: Alice and Bob are in a relationship, as are Carol and David. However, Alice and David had a bad breakup a while ago and haven't been civil with each other since. Expression: ((Alice & Bob) | (Carol & David)) & ~(Alice and David) Situation: {situation} Expression:""" prompt_template = PromptTemplate( input_variables=["situation"], template=template, ) chain = LLMChain(llm=llm, prompt=prompt_template) **C:\Users\test\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\_api\deprecation.py:189: LangChainDeprecationWarning: The class `OpenAI` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use langchain_openai.OpenAI instead. warn_deprecated(** from qiskit.algorithms import AmplificationProblem, Grover from qiskit.circuit.library import PhaseOracle from qiskit import Aer from qiskit.visualization import plot_histogram **C:\Users\test\AppData\Local\Temp\ipykernel_41764\3470102279.py:1: DeprecationWarning: ``qiskit.algorithms`` has been migrated to an independent package: https://github.com/qiskit-community/qiskit-algorithms. The ``qiskit.algorithms`` import path is deprecated as of qiskit-terra 0.25.0 and will be removed no earlier than 3 months after the release date. Please run ``pip install qiskit_algorithms`` and use ``import qiskit_algorithms`` instead. from qiskit.algorithms import AmplificationProblem, Grover** situation = """There are four people, Abe, Amira, Jin, Olivia. Abe and Olivia are good friends from Qiskit Camp. Abe and Amira just had a big fight and don't want to hang""" bool_expr = chain.run(situation).strip() print(bool_expr) oracle = PhaseOracle(bool_expr) problem = AmplificationProblem(oracle) **C:\Users\test\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\_api\deprecation.py:189: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead. warn_deprecated( C:\Users\test\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\_api\deprecation.py:189: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead. warn_deprecated(** backend = Aer.get_backend("aer_simulator") grover = Grover(quantum_instance=backend) result = grover.amplify(problem) counts = result.circuit_results plot_histogram(counts) **--------------------------------------------------------------------------- ImportError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\qiskit\__init__.py:123, in AerWrapper.__getattr__(self, attr) 122 try: --> 123 from qiskit.providers import aer 125 self.aer = aer.Aer ImportError: cannot import name 'aer' from 'qiskit.providers' (C:\Users\test\AppData\Local\Programs\Python\Python310\lib\site-packages\qiskit\providers\__init__.py) The above exception was the direct cause of the following exception: MissingOptionalLibraryError Traceback (most recent call last) Cell In[6], line 1 ----> 1 backend = Aer.get_backend("aer_simulator") 3 grover = Grover(quantum_instance=backend) 4 result = grover.amplify(problem) File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\qiskit\__init__.py:134, in AerWrapper.__getattr__(self, attr) 126 warnings.warn( 127 "The qiskit.Aer entry point will be deprecated in a future release and " 128 "subsequently removed. Instead you should use this " (...) 131 stacklevel=2, 132 ) 133 except ImportError as ex: --> 134 raise MissingOptionalLibraryError( 135 "qiskit-aer", "Aer provider", "pip install qiskit-aer" 136 ) from ex 137 return getattr(self.aer, attr) MissingOptionalLibraryError: "The 'qiskit-aer' library is required to use 'Aer provider'. You can install it with 'pip install qiskit-aer'."**
kozmando commented 10 months ago

Asked QuantumGPT and it gave a working update for the 5th section, which makes the program run without error. However, the boolean logic is slightly different.

situation = """There are four people: Abe, Amira, Jin, Olivia. Abe and Olivia are good friends from Qiskit Camp. Abe and Amira just had a big fight and don't want to hang. I can only invite three people to the party.""" result = chain.invoke(situation)

Extract the boolean expression using the 'text' key and apply strip() to it

bool_expr = result['text'].strip() print(bool_expr)

Use the boolean expression to create the oracle

oracle = PhaseOracle(bool_expr)

Create the AmplificationProblem with the oracle

problem = AmplificationProblem(oracle) print(problem)

Continue with the rest of your quantum computing code...