Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output from sample should be ordered #2158

Open
1 task done
zohimchandani opened this issue Aug 27, 2024 · 2 comments
Open
1 task done

Output from sample should be ordered #2158

zohimchandani opened this issue Aug 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@zohimchandani
Copy link
Collaborator

Required prerequisites

  • Search the issue tracker to check if your feature has already been mentioned or rejected in other issues.

Describe the feature

@cudaq.kernel 
def psi(): 
    qubits = cudaq.qvector(3)
    
    h(qubits[0])
    h(qubits[1])
    h(qubits[2])
    

counts = cudaq.sample(psi)
print(counts)
{ 110:144 101:121 100:131 011:103 001:132 111:118 010:129 000:122 }

Can the output be ordered according to binary notation of quantum states ie:

0 = 000 = 22 * 0 + 21 * 0 + 2**0 * 0 = 0
1 = 001
2 = 010
3 = 011

etc

@zohimchandani zohimchandani added the enhancement New feature or request label Aug 27, 2024
@sacpis
Copy link
Collaborator

sacpis commented Aug 31, 2024

For the above code, do we want the order of the sample output like this

{ 111:118 110:144 101:121 100:131 011:103 010:129 001:132 000:122 }

or

{ 000:122 001:132 010:129 011:103 100:131 101:121 110:144 111:118 }?

@ikkoham
Copy link
Contributor

ikkoham commented Sep 3, 2024

Sorting of exponential sized data is costly and is not recommended to be sorted all the time.
It may be optional, but can be easily implemented as follows

import cudaq

@cudaq.kernel 
def psi(): 
    qubits = cudaq.qvector(3)
    
    h(qubits[0])
    h(qubits[1])
    h(qubits[2])

counts = cudaq.sample(psi)
sorted_counts = sorted(counts.items(), key=lambda x:x[0])
print(sorted_counts)

[('000', 131), ('001', 129), ('010', 105), ('011', 139), ('100', 139), ('101', 107), ('110', 131), ('111', 119)]

or

import cudaq

@cudaq.kernel 
def psi(): 
    qubits = cudaq.qvector(3)
    
    h(qubits[0])
    h(qubits[1])
    h(qubits[2])

counts = cudaq.sample(psi)
sorted_counts = sorted(counts.items(), key=lambda x:x[0][::-1])
print(sorted_counts)

[('000', 140), ('100', 123), ('010', 114), ('110', 119), ('001', 135), ('101', 137), ('011', 117), ('111', 115)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants