amazon-braket / amazon-braket-examples

Example notebooks that show how to apply quantum computing with Amazon Braket.
https://aws.amazon.com/braket/
Apache License 2.0
463 stars 224 forks source link

Solve an annealing problem with `Device.run` #66

Closed teaguetomesh closed 3 years ago

teaguetomesh commented 3 years ago

In the D-Wave MaxCut example, an AwsDevice is created in the 4th code block:

device = AwsDevice("arn:aws:braket:::device/qpu/d-wave/Advantage_system1")

However, device is never used again in the notebook. Instead, the MaxCut problem is solved using a sampler object:

sampler = BraketDWaveSampler(s3_folder,'arn:aws:braket:::device/qpu/d-wave/Advantage_system1')
sampler = EmbeddingComposite(sampler)
response = sampler.sample_qubo(Q, chain_strength=chainstrength, num_reads=numruns)

Is this the only way to use the D-Wave devices and solve annealing problems? Or is it possible to use the device.run() function? The run function has the option of passing in a Problem as the task specification (instead of a Circuit), but I have so far been unable to get this to work.

Any help or directions to relevant tutorials would be greatly appreciated!

wrasmuss commented 3 years ago

@teaguetomesh Thanks for pointing out the unneeded creation of the AwsDevice instance.

As far solving a problem with AwsDevice.run it is definitely possible but it is less streamlined and we generally encourage the Ocean plugin route. Perhaps if you can shed some light on your use case we can make some recommendations.

teaguetomesh commented 3 years ago

Hi @wrasmuss, thanks for taking a look at this issue. For our use case we have both AwsDevice and Problem objects constructed and would like to find a solution to that QUBO problem by calling the AwsDevice.run function -- this would (1) let us implement this capability without needing to add support for another software package (the Ocean plugin), and (2) give us more flexibility in the backend we use to solve the QUBO.

How different are the run vs plugin methods for solving these QUBO problems? And how much effort would it take to bring them up to speed?

ajberdy commented 3 years ago

Hi @teaguetomesh, here is a minimal example for finding a solution to a QUBO problem using AwsDevice.run:

s3_folder = ("amazon-braket-Your-Bucket-Name", "folder-name")

device = AwsDevice("arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6")
problem = Problem(
    problem_type=ProblemType.QUBO,
    linear={30: 0.0},
    quadratic={},
)

task = device.run(problem, s3_folder)
print(task.result())
teaguetomesh commented 3 years ago

Thanks for the example. The above code runs fine for me, but trying a problem with quadratic terms seems to be breaking:

problem = Problem(
    problem_type=ProblemType.QUBO,
    linear={0: 1.1, 1: 0.8},
    quadratic={(0, 1): -2.0},
)

Running this problem gives an error:

ValidationException: An error occurred (ValidationException) when calling the CreateQuantumTask operation: Unknown couplers specified: {(0, 1)}

But this is how the quadratic terms should be represented, no?

ajberdy commented 3 years ago

Hi @teaguetomesh, you are using the correct format. The issue is that (0, 1) is not a valid coupler for the device you are using. You can see all valid couplers for a D-Wave device with device.properties.provider.couplers. The two chips have different topologies (device.properties.provider.topology) and hence different sets of qubits and couplers. Take a look at D-Wave's documentation of their device's topology if you'd like to learn more!

Your example works if you select 2 qubits from a valid coupler:

device = AwsDevice("arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6")

problem = Problem(
    problem_type=ProblemType.QUBO,
    linear={0: 1.1, 4: 0.8},
    quadratic={(0, 4): -2.0},
)
task = device.run(problem, s3_folder)
print(task.result())
teaguetomesh commented 3 years ago

I see, thank you this is super helpful!

Are there functions available within Braket that could do this mapping for me? Specifically, taking a generic Problem on variables [0, 1, 2,...n] and map them to valid couplers on the D-Wave devices?

ajberdy commented 3 years ago

The sampler featured in the braket ocean plugin is designed to abstract these sorts of things and will generally be more convenient to work with on a higher level. The example notebooks provide examples for its use. Meanwhile, AwsDevice is a lower-level construct for directly running circuits and problems on a device through Braket, without the additional preprocessing.

teaguetomesh commented 3 years ago

I see - in that case I suppose it makes more sense to utilize the samplers from the Ocean plugin for our use case. Thanks for the help!