-
Notifications
You must be signed in to change notification settings - Fork 3k
Use rust gates for ConsolidateBlocks #12704
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
Changes from all commits
6567728
56a21cd
032bf18
a3ffebe
0a50be9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ | |
| // copyright notice, and modified files need to carry a notice indicating | ||
| // that they have been altered from the originals. | ||
|
|
||
| use pyo3::intern; | ||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyDict; | ||
| use pyo3::wrap_pyfunction; | ||
| use pyo3::Python; | ||
|
|
||
|
|
@@ -20,32 +22,84 @@ use numpy::ndarray::{aview2, Array2, ArrayView2}; | |
| use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2}; | ||
| use smallvec::SmallVec; | ||
|
|
||
| use qiskit_circuit::bit_data::BitData; | ||
| use qiskit_circuit::circuit_instruction::{operation_type_to_py, CircuitInstruction}; | ||
| use qiskit_circuit::dag_node::DAGOpNode; | ||
| use qiskit_circuit::gate_matrix::ONE_QUBIT_IDENTITY; | ||
| use qiskit_circuit::imports::QI_OPERATOR; | ||
| use qiskit_circuit::operations::{Operation, OperationType}; | ||
|
|
||
| use crate::QiskitError; | ||
|
|
||
| fn get_matrix_from_inst<'py>( | ||
| py: Python<'py>, | ||
| inst: &'py CircuitInstruction, | ||
| ) -> PyResult<Array2<Complex64>> { | ||
| match inst.operation.matrix(&inst.params) { | ||
| Some(mat) => Ok(mat), | ||
| None => match inst.operation { | ||
| OperationType::Standard(_) => Err(QiskitError::new_err( | ||
| "Parameterized gates can't be consolidated", | ||
| )), | ||
| OperationType::Gate(_) => Ok(QI_OPERATOR | ||
| .get_bound(py) | ||
| .call1((operation_type_to_py(py, inst)?,))? | ||
| .getattr(intern!(py, "data"))? | ||
| .extract::<PyReadonlyArray2<Complex64>>()? | ||
| .as_array() | ||
| .to_owned()), | ||
| _ => unreachable!("Only called for unitary ops"), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Return the matrix Operator resulting from a block of Instructions. | ||
| #[pyfunction] | ||
| #[pyo3(text_signature = "(op_list, /")] | ||
| pub fn blocks_to_matrix( | ||
| py: Python, | ||
| op_list: Vec<(PyReadonlyArray2<Complex64>, SmallVec<[u8; 2]>)>, | ||
| op_list: Vec<PyRef<DAGOpNode>>, | ||
| block_index_map_dict: &Bound<PyDict>, | ||
| ) -> PyResult<Py<PyArray2<Complex64>>> { | ||
| // Build a BitData in block_index_map_dict order. block_index_map_dict is a dict of bits to | ||
| // indices mapping the order of the qargs in the block. There should only be 2 entries since | ||
| // there are only 2 qargs here (e.g. `{Qubit(): 0, Qubit(): 1}`) so we need to ensure that | ||
| // we added the qubits to bit data in the correct index order. | ||
| let mut index_map: Vec<PyObject> = (0..block_index_map_dict.len()).map(|_| py.None()).collect(); | ||
| for bit_tuple in block_index_map_dict.items() { | ||
| let (bit, index): (PyObject, usize) = bit_tuple.extract()?; | ||
| index_map[index] = bit; | ||
| } | ||
| let mut bit_map: BitData<u32> = BitData::new(py, "qargs".to_string()); | ||
| for bit in index_map { | ||
| bit_map.add(py, bit.bind(py), true)?; | ||
| } | ||
| let identity = aview2(&ONE_QUBIT_IDENTITY); | ||
| let input_matrix = op_list[0].0.as_array(); | ||
| let mut matrix: Array2<Complex64> = match op_list[0].1.as_slice() { | ||
| let first_node = &op_list[0]; | ||
| let input_matrix = get_matrix_from_inst(py, &first_node.instruction)?; | ||
| let mut matrix: Array2<Complex64> = match bit_map | ||
| .map_bits(first_node.instruction.qubits.bind(py).iter())? | ||
| .collect::<Vec<_>>() | ||
| .as_slice() | ||
| { | ||
| [0] => kron(&identity, &input_matrix), | ||
| [1] => kron(&input_matrix, &identity), | ||
| [0, 1] => input_matrix.to_owned(), | ||
| [1, 0] => change_basis(input_matrix), | ||
| [0, 1] => input_matrix, | ||
| [1, 0] => change_basis(input_matrix.view()), | ||
| [] => Array2::eye(4), | ||
| _ => unreachable!(), | ||
| }; | ||
| for (op_matrix, q_list) in op_list.into_iter().skip(1) { | ||
| let op_matrix = op_matrix.as_array(); | ||
| for node in op_list.into_iter().skip(1) { | ||
| let op_matrix = get_matrix_from_inst(py, &node.instruction)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 91 would convey more information as |
||
| let q_list = bit_map | ||
| .map_bits(node.instruction.qubits.bind(py).iter())? | ||
| .map(|x| x as u8) | ||
| .collect::<SmallVec<[u8; 2]>>(); | ||
|
|
||
| let result = match q_list.as_slice() { | ||
| [0] => Some(kron(&identity, &op_matrix)), | ||
| [1] => Some(kron(&op_matrix, &identity)), | ||
| [1, 0] => Some(change_basis(op_matrix)), | ||
| [1, 0] => Some(change_basis(op_matrix.view())), | ||
| [] => Some(Array2::eye(4)), | ||
| _ => None, | ||
| }; | ||
|
|
@@ -71,8 +125,42 @@ pub fn change_basis(matrix: ArrayView2<Complex64>) -> Array2<Complex64> { | |
| trans_matrix | ||
| } | ||
|
|
||
| #[pyfunction] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 50 could be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two suggestions, doing |
||
| pub fn collect_2q_blocks_filter(node: &Bound<PyAny>) -> Option<bool> { | ||
| match node.downcast::<DAGOpNode>() { | ||
| Ok(bound_node) => { | ||
| let node = bound_node.borrow(); | ||
| match &node.instruction.operation { | ||
| OperationType::Standard(gate) => Some( | ||
| gate.num_qubits() <= 2 | ||
| && node | ||
| .instruction | ||
| .extra_attrs | ||
| .as_ref() | ||
| .and_then(|attrs| attrs.condition.as_ref()) | ||
| .is_none() | ||
| && !node.is_parameterized(), | ||
| ), | ||
| OperationType::Gate(gate) => Some( | ||
| gate.num_qubits() <= 2 | ||
| && node | ||
| .instruction | ||
| .extra_attrs | ||
| .as_ref() | ||
| .and_then(|attrs| attrs.condition.as_ref()) | ||
| .is_none() | ||
| && !node.is_parameterized(), | ||
| ), | ||
| _ => Some(false), | ||
| } | ||
| } | ||
| Err(_) => None, | ||
| } | ||
| } | ||
|
|
||
| #[pymodule] | ||
| pub fn convert_2q_block_matrix(m: &Bound<PyModule>) -> PyResult<()> { | ||
| m.add_wrapped(wrap_pyfunction!(blocks_to_matrix))?; | ||
| m.add_wrapped(wrap_pyfunction!(collect_2q_blocks_filter))?; | ||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,4 +31,3 @@ | |
|
|
||
| # Utility functions | ||
| from . import control_flow | ||
| from .block_to_matrix import _block_to_matrix | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.