Solved: How to find out the number of qubits used to solve a problem

Hello, I am trying to optimize my python code to use as few qubits as possible.

I want to check how many qubits were used in order to solve each problem. Is it possible through either the dwave.system module or the Leap problem screen? I tried reading through the documentation, but couldn't find anything, and I'm not really used to the problem status screen to decipher it.

Thanks in advance

Update: The answer provided by Ed works, but you need to import dwave.inspector for it to work correctly

1

Comments

8 comments
  • Evangelos,

    To see how many qubits are used to solve a problem, you can use the following code:

    #use the sampler of your choosing for getting sampleset

    sampleset = sampler.sample(model.to_bqm(), num_reads=num_reads)

    embedding = sampleset.info['embedding_context']['embedding']

    print(f"Number of logical variables: {len(embedding.keys())}")
    print(f"Number of physical qubits used in embedding: {sum(len(chain) for chain in embedding.values())}")

    The number of physical qubits will give you the number you asked for.

    Note: some embedding algorithms are probabalistic, so you may get a different number of qubits for each run, even though you have not changed the problem.

    Two posts which might be helpful are:

    https://support.dwavesys.com/hc/en-us/community/posts/360036439654-Embedding-Information

    https://support.dwavesys.com/hc/en-us/community/posts/360014245253-Factoring-program-embedding-metrics

    Ed

    1
    Comment actions Permalink
  • Hello Ed,

    I tried using the code you provided but came across another problem, the SampleSet object that is returned by sample(bqm, num_reads=n_reads) does not include the key 'embedding_context'. Printing the objects yielded this:

    {'timing': {'qpu_sampling_time': 174920, 'qpu_anneal_time_per_sample': 20, 'qpu_readout_time_per_sample': 134, 'qpu_access_time': 190725, 'qpu_access_overhead_time': 18834, 'qpu_programming_time': 15805, 'qpu_delay_time_per_sample': 21, 'total_post_processing_time': 30430, 'post_processing_overhead_time': 658}, 'problem_id': '7c29f32f-1786-49a9-88fe-50dc22de1e2e'}

    I'm guessing that the 'embedding_context; would be another key in the dict and that for some reason it's missing, do you have any guesses as to why?

    This is the code I'm using to create the sampler:

    sampler = EmbeddingComposite(DWaveSampler())

     res = sampler.sample(dimod.BQM.from_qubo(qubo), num_reads=n_reads)
    embedding = res.info['embedding_context']['embedding']

    Evangelos

    0
    Comment actions Permalink
  • Evangelos,

    The code I used yesterday for testing this is not working today either. I'm not sure if D-Wave is making some changes to their solver or if something else is going on.

    If someone else from D-Wave knows, perhaps they could let us know. I am sure that I was getting good results with the code I posted earlier. If it is something wrong with my code, I would like to know that as well.

    Ed

    1
    Comment actions Permalink
  • UPDATE: My code is working. I had changed it to not use the qpu for the solver (trying to save on my QPU time budget), so it did not have an embedding. Once I used a QPU solver, the embedding information returned.

    Still not sure why it is not working for you. I use the Pyqubo module to create my models, but am using the D-Wave samplers.

    Ed

    0
    Comment actions Permalink
  • Evangelos,

    I am out of ideas, perhaps someone else on the forum would know why the embedding context is not present.

    Here is an example from D-Wave which shows it being used:

    https://docs.dwavesys.com/docs/latest/handbook_embedding.html#id6

    Good luck.

    Ed

    0
    Comment actions Permalink
  • Ed,

    Thanks a lot for your help, and for going through all this trouble.

    I'll keep looking if I messed something up or if it's a problem with my account(?). If anyone from D-Wave knows why "embedding_context" is missing and can help out that'd be great.

    Thanks again,

    Evangelos

    0
    Comment actions Permalink
  • UPDATE: I found the solution!

    I hadn't imported dwave.inspector.

    I'm not really sure what it does but now my program thankfully works! Thanks again Ed, without the example you gave me I wouldn't have found it!

    Evangelos

     

    0
    Comment actions Permalink
  • That's great. Thanks for sharing what fixed this. I had always assumed that the dwave.inspector import was only used to allow the inspector GUI to be invoked (which is why I keep it in the code I use for testing new ideas and code snippets). Apparently, it also augments the results from the sampler.

    Good luck on minimizing the number of qubits needed.

    Ed

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post