Improve custom unitary operations
Open
Nobody has claimed this yet.
enhancement
stale-notified
- Dominant language
- C++
- Stars
- 1.1k
- Forks
- 456
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 165
Description
Required prerequisites
- Search the issue tracker to check if your feature has already been mentioned or rejected in other issues.
Describe the feature
Improvement suggestions for custom unitary operations:
- Allow unitary operations to be passed a qvector as a parameter
- Support kernel mode.
- Support lookup of custom unitary ops with parameterized strings (see #2137 for details)
Example of code where those features would be beneficial:
import cudaq
import numpy as np
@cudaq.kernel
def kernel_helper(state: list[complex]):
qubits = cudaq.qvector(state)
def flip_qubit(matrix, i):
j = i - 1
idx = 1 << i
if (j < 0):
jdx = 0
else:
jdx = 1 << j
matrix[jdx][jdx] = 0
matrix[idx][idx] = 0
matrix[idx][jdx] = 1
matrix[jdx][idx] = 1
return matrix
def test_unitary():
def register_custom_operation(unitary_index: int, matrix: np.ndarray):
cudaq.register_operation(f"U_{unitary_index}", matrix)
state = np.zeros(8)
state[0] = 1
state = np.array(state, dtype=cudaq.complex())
states = [state]
for i in range(0,3):
matrix = flip_qubit(np.identity(8), i)
register_custom_operation(i, matrix)
kernel, s = cudaq.make_kernel(cudaq.State)
qubits = kernel.qalloc(s)
kernel.__getattr__(f'U_{i}')(qubits[0], qubits[1], qubits[2])
cudaq_state = cudaq.State.from_data(state)
state = np.array(cudaq.get_state(kernel, cudaq_state))
states.append(state)
for s in states:
counts = cudaq.sample(kernel_helper, s)
print(counts)
test_unitary()
# Output:
# { 000:1000 }
# { 001:1000 }
# { 010:1000 }
# { 100:1000 }
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example using cudaq.register_operation, cudaq.make_kernel, and cudaq.sample, then trace how custom unitary operations are registered and invoked. Review the behavior required for qvector parameters, kernel mode, and parameterized operation lookup as described in the issue and #2137. Done means the listed use cases work and the example produces the expected states.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- quantum-computing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100