NVIDIA / cuQuantum

Home for cuQuantum Python & NVIDIA cuQuantum SDK C++ samples
https://docs.nvidia.com/cuda/cuquantum/
BSD 3-Clause "New" or "Revised" License
320 stars 63 forks source link

Support CUDA-Q kernels for CircuitToEinsum #141

Open zohimchandani opened 2 weeks ago

zohimchandani commented 2 weeks ago

For an upcoming demo, we need cuQuantum to accept CUDA-Q circuits into the CircuitToEinsum function here.

Here is an example of how to do this in Qiskit:

from cuquantum import cutensornet as cutn
from cuquantum import contract, CircuitToEinsum
import torch 
from qiskit.circuit import QuantumCircuit, Parameter

num_qubits = 1
measurement_ops = 'Z'
light_cone  = True 

# Define the quantum circuit with one qubit and two parameters
theta = Parameter('θ')
phi = Parameter('φ')

circuit = QuantumCircuit(num_qubits)
circuit.rx(theta, 0)
circuit.ry(phi, 0)

parameters = [1,2]

circuit = circuit.bind_parameters({theta: parameters[0], phi: parameters[1]})

converter = CircuitToEinsum(circuit, backend='torch')  

einsum_expression, tensor_operands = converter.expectation(measurement_ops, light_cone)

Note that you have to provide a bound circuit (circuit with values supplied for the variational gates).

It would be nice if we could support CircuitToEinsum to take in variational circuits that are unbound but I'm guessing that the einsum expression will look different for different parameter values and hence this is not feasible?

In CUDA-Q, the corresponding code looks like this:

import cudaq
from typing import List

cudaq.set_target("nvidia")

@cudaq.kernel
def kernel(angles: List[float]):
    qubit = cudaq.qubit()
    rx(angles[0], qubit)
    ry(angles[1], qubit)

hamiltonian = cudaq.spin.z(0)

parameters = [1, 2]

expectation_value = cudaq.observe(kernel, hamiltonian, parameters).expectation()

Note how the binding of the parameters happens in the observe call. How do we deal with this? @1tnguyen @bettinaheim

bettinaheim commented 2 weeks ago

@Zohim @.***> When is the upcoming demo and what is it for?

For the cuQuantum team: how does the conversation work, i.e. when you get a circuit object, what do you do with it?

From: Zohim Chandani @.> Sent: Saturday, 15 June 2024 16:09 To: NVIDIA/cuQuantum @.> Cc: Bettina Heim @.>; Mention @.> Subject: [NVIDIA/cuQuantum] Support CUDA-Q kernels for CircuitToEinsum (Issue #141)

For an upcoming demo, we need cuQuantum to accept CUDA-Q circuits into the CircuitToEinsum function herehttps://docs.nvidia.com/cuda/cuquantum/latest/python/api/generated/cuquantum.CircuitToEinsum.html?highlight=circuittoeinsum#:~:text=The%20supported%20circuit%20types%20include%20cirq.Circuit%20and%20qiskit.QuantumCircuit.%20The%20input%20circuit%20must%20be%20fully%20parameterized%20and%20can%20not%20contain%20operations%20that%20are%20not%20well-defined%20in%20tensor%20network%20simulation%2C%20for%20instance%2C%20resetting%20the%20quantum%20state%20or%20performing%20any%20intermediate%20measurement..

Here is an example of how to do this in Qiskit:

from cuquantum import cutensornet as cutn

from cuquantum import contract, CircuitToEinsum

import torch

from qiskit.circuit import QuantumCircuit, Parameter

num_qubits = 1

measurement_ops = 'Z'

light_cone = True

Define the quantum circuit with one qubit and two parameters

theta = Parameter('θ')

phi = Parameter('φ')

circuit = QuantumCircuit(num_qubits)

circuit.rx(theta, 0)

circuit.ry(phi, 0)

parameters = [1,2]

circuit = circuit.bind_parameters({theta: parameters[0], phi: parameters[1]})

converter = CircuitToEinsum(circuit, backend='torch')

einsum_expression, tensor_operands = converter.expectation(measurement_ops, light_cone)

Note that you have to provide a bound circuit (circuit with values supplied for the variational gates).

It would be nice if we could support CircuitToEinsum to take in variational circuits that are unbound but I'm guessing that the einsum expression will look different for different parameter values and hence this is not feasible?

In CUDA-Q, the corresponding code looks like this:

import cudaq

from typing import List

cudaq.set_target("nvidia")

@cudaq.kernel

def kernel(angles: List[float]):

qubit = cudaq.qubit()

rx(angles[0], qubit)

ry(angles[1], qubit)

hamiltonian = cudaq.spin.z(0)

parameters = [1, 2]

expectation_value = cudaq.observe(kernel, hamiltonian, parameters).expectation()

Note how the binding of the parameters happens in the observe call. How do we deal with this? @1tnguyenhttps://github.com/1tnguyen @bettinaheimhttps://github.com/bettinaheim

— Reply to this email directly, view it on GitHubhttps://github.com/NVIDIA/cuQuantum/issues/141, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIFGON4JTEEFEXYLCQ6QK2TZHRDJLAVCNFSM6AAAAABJLXFCLCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM2TIOBYGIZDGMQ. You are receiving this because you were mentioned.Message ID: @.**@.>>

zohimchandani commented 2 weeks ago

Demo is scheduled for 15th August, we need to give this feature to our collaborators so that they can implement the workflow ahead of this. Thanks team.

fmozafari commented 2 weeks ago

@bettinaheim @zohimchandani In circuitToEinsum we go over a circuit object and extract a sequence of gates with their matrix representations and qubits that act on. We tried to add cudaq but seems there is not explicit python API to get the matrices. Even we can not directly get the gate names and it is required to parse strings from kernel.to_qir(). Is it possible to add this functionality to get the matrix for each gate?

zohimchandani commented 5 days ago

@bettinaheim pinging as a reminder, thanks.