amazon-braket-sdk-python icon indicating copy to clipboard operation
amazon-braket-sdk-python copied to clipboard

Add `random_circuit` function for testing

Open ryanhill1 opened this issue 3 years ago • 7 comments

Describe the feature you'd like Function that generates a "random" Braket circuit. I looked through the code base and couldn't find one, but if it already exists it would be great to know where!

How would this feature be used? Please describe. Mainly for testing purposes in projects that are built using Braket. Would be helpful for verifying coverage over all (or at least many) Braket gates/operations.

Describe alternatives you've considered Tried converting Cirq random circuit and Qiskit random circuit objects to Braket using the qBraid transpiler, but this method, by nature, generalizes the circuit and so lacks coverage.

ryanhill1 avatar Jan 19 '23 22:01 ryanhill1

Hi @ryanhill1, thanks for the feature request. You're correct there is not currently a random circuit function in the SDK.

Below is some code to randomly select single-qubit gates and create a circuit. It should be possible to generalize to two-qubit gates. Would this cover your use-case?

import random
from braket.circuits import Circuit, Instruction
from braket.circuits.gates import I,X,Y,Z,H,S,Si,V,Vi,Rx,Ry,Rz, GPi,GPi2


def random_circuit(num_qubits:int, num_instructions:int) -> Circuit:
    gate_set = [I(), X(), Y(), Z(),H(),S(),Si(),V(),Vi()]
    gate_set += [Rx(0.5),Ry(0.5),Rz(0.5), GPi(0.5), GPi2(0.5)]
    
    circ = Circuit()
    for _ in range(num_instructions):
        q = random.choice(range(num_qubits))
        gate = random.choice(gate_set)
        circ.add(Instruction(gate, [q]))
    return circ

print(random_circuit(2,10))

Let me know if this works for you.

mbeach-aws avatar Feb 20 '23 20:02 mbeach-aws

@mbeach-aws Thanks for your response! Yes, for now this works. Is this feature something your team would be interested in adding? If so, I can help with a draft PR working from your single-qubit gate example above

ryanhill1 avatar Mar 09 '23 19:03 ryanhill1

Hi @ryanhill1, I think it would actually be more useful here: https://github.com/aws-samples/amazon-braket-algorithm-library which is more focused on Braket sample code. The algorithm library is meant for exactly this type of thing where a user could quickly load up an example circuit, like random circuits. It would also be cool if there was a notebook explaining a bit about the usefulness of random circuits, like maybe quantum volume, or benchmarking.

mbeach-aws avatar Mar 09 '23 20:03 mbeach-aws

@ryanhill1 are you still interested in drafting a PR here but in the https://github.com/aws-samples/amazon-braket-algorithm-library repository ad @mbeach-aws suggested?

christianbmadsen avatar May 23 '23 14:05 christianbmadsen

Hi @christianbmadsen, thanks for reaching out! I've got my hands full at the moment getting the qBraid-SDK ready for Unitary Hack. But after the event is over and the PRs die down I'll have some time and could help with a draft 👍

But if you all had a faster turn-around in mind I'd say go for it, and I can jump back in if any help is needed.

ryanhill1 avatar May 24 '23 01:05 ryanhill1

Hey @christianbmadsen, @kshitijc and @mbeach-aws,

Just came across this discussion about the random circuit function for Amazon Braket. I really like the idea and I'm keen on helping out with it. The code snippet you shared, @mbeach-aws, looks like a great starting point.

Should I go ahead and open an issue in the Braket Algorithm Library repo to get things rolling? Happy to contribute and collaborate on this!

Morcu avatar Nov 30 '23 16:11 Morcu

Thanks @Morcu! That would be fantastic. Please go ahead and open the PR in the Braket algorithm library.

kshitijc avatar Nov 30 '23 20:11 kshitijc