|
| 1 | +// This code is part of Qiskit. |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2024 |
| 4 | +// |
| 5 | +// This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +// obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// Any modifications or derivative works of this code must retain this |
| 10 | +// copyright notice, and modified files need to carry a notice indicating |
| 11 | +// that they have been altered from the originals. |
| 12 | + |
| 13 | +use pyo3::prelude::*; |
| 14 | + |
| 15 | +use crate::getenv_use_multiple_threads; |
| 16 | +use faer_ext::{IntoFaerComplex, IntoNdarrayComplex}; |
| 17 | +use ndarray::prelude::*; |
| 18 | +use num_complex::Complex64; |
| 19 | +use numpy::IntoPyArray; |
| 20 | +use rand::prelude::*; |
| 21 | +use rand_distr::StandardNormal; |
| 22 | +use rand_pcg::Pcg64Mcg; |
| 23 | +use rayon::prelude::*; |
| 24 | + |
| 25 | +use qiskit_circuit::circuit_data::CircuitData; |
| 26 | +use qiskit_circuit::imports::UNITARY_GATE; |
| 27 | +use qiskit_circuit::operations::Param; |
| 28 | +use qiskit_circuit::operations::PyInstruction; |
| 29 | +use qiskit_circuit::packed_instruction::PackedOperation; |
| 30 | +use qiskit_circuit::{Clbit, Qubit}; |
| 31 | +use smallvec::{smallvec, SmallVec}; |
| 32 | + |
| 33 | +type Instruction = ( |
| 34 | + PackedOperation, |
| 35 | + SmallVec<[Param; 3]>, |
| 36 | + Vec<Qubit>, |
| 37 | + Vec<Clbit>, |
| 38 | +); |
| 39 | + |
| 40 | +#[inline(always)] |
| 41 | +fn random_complex(rng: &mut Pcg64Mcg) -> Complex64 { |
| 42 | + Complex64::new(rng.sample(StandardNormal), rng.sample(StandardNormal)) |
| 43 | + * std::f64::consts::FRAC_1_SQRT_2 |
| 44 | +} |
| 45 | + |
| 46 | +// This function's implementation was modeled off of the algorithm used in the |
| 47 | +// `scipy.stats.unitary_group.rvs()` function defined here: |
| 48 | +// |
| 49 | +// https://github.com/scipy/scipy/blob/v1.14.1/scipy/stats/_multivariate.py#L4224-L4256 |
| 50 | +#[inline] |
| 51 | +fn random_unitaries(seed: u64, size: usize) -> impl Iterator<Item = Array2<Complex64>> { |
| 52 | + let mut rng = Pcg64Mcg::seed_from_u64(seed); |
| 53 | + |
| 54 | + (0..size).map(move |_| { |
| 55 | + let raw_numbers: [[Complex64; 4]; 4] = [ |
| 56 | + [ |
| 57 | + random_complex(&mut rng), |
| 58 | + random_complex(&mut rng), |
| 59 | + random_complex(&mut rng), |
| 60 | + random_complex(&mut rng), |
| 61 | + ], |
| 62 | + [ |
| 63 | + random_complex(&mut rng), |
| 64 | + random_complex(&mut rng), |
| 65 | + random_complex(&mut rng), |
| 66 | + random_complex(&mut rng), |
| 67 | + ], |
| 68 | + [ |
| 69 | + random_complex(&mut rng), |
| 70 | + random_complex(&mut rng), |
| 71 | + random_complex(&mut rng), |
| 72 | + random_complex(&mut rng), |
| 73 | + ], |
| 74 | + [ |
| 75 | + random_complex(&mut rng), |
| 76 | + random_complex(&mut rng), |
| 77 | + random_complex(&mut rng), |
| 78 | + random_complex(&mut rng), |
| 79 | + ], |
| 80 | + ]; |
| 81 | + |
| 82 | + let qr = aview2(&raw_numbers).into_faer_complex().qr(); |
| 83 | + let r = qr.compute_r(); |
| 84 | + let diag: [Complex64; 4] = [ |
| 85 | + r[(0, 0)].to_num_complex() / r[(0, 0)].abs(), |
| 86 | + r[(1, 1)].to_num_complex() / r[(1, 1)].abs(), |
| 87 | + r[(2, 2)].to_num_complex() / r[(2, 2)].abs(), |
| 88 | + r[(3, 3)].to_num_complex() / r[(3, 3)].abs(), |
| 89 | + ]; |
| 90 | + let mut q = qr.compute_q().as_ref().into_ndarray_complex().to_owned(); |
| 91 | + q.axis_iter_mut(Axis(0)).for_each(|mut row| { |
| 92 | + row.iter_mut() |
| 93 | + .enumerate() |
| 94 | + .for_each(|(index, val)| *val *= diag[index]) |
| 95 | + }); |
| 96 | + q |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +const UNITARY_PER_SEED: usize = 50; |
| 101 | + |
| 102 | +#[pyfunction] |
| 103 | +pub fn quantum_volume( |
| 104 | + py: Python, |
| 105 | + num_qubits: u32, |
| 106 | + depth: usize, |
| 107 | + seed: Option<u64>, |
| 108 | +) -> PyResult<CircuitData> { |
| 109 | + let width = num_qubits as usize / 2; |
| 110 | + let num_unitaries = width * depth; |
| 111 | + let mut permutation: Vec<Qubit> = (0..num_qubits).map(Qubit).collect(); |
| 112 | + |
| 113 | + let mut build_instruction = |(unitary_index, unitary_array): (usize, Array2<Complex64>), |
| 114 | + rng: &mut Pcg64Mcg| |
| 115 | + -> PyResult<Instruction> { |
| 116 | + let layer_index = unitary_index % width; |
| 117 | + if layer_index == 0 { |
| 118 | + permutation.shuffle(rng); |
| 119 | + } |
| 120 | + let unitary = unitary_array.into_pyarray_bound(py); |
| 121 | + let unitary_gate = UNITARY_GATE |
| 122 | + .get_bound(py) |
| 123 | + .call1((unitary.clone(), py.None(), false))?; |
| 124 | + let instruction = PyInstruction { |
| 125 | + qubits: 2, |
| 126 | + clbits: 0, |
| 127 | + params: 1, |
| 128 | + op_name: "unitary".to_string(), |
| 129 | + control_flow: false, |
| 130 | + instruction: unitary_gate.unbind(), |
| 131 | + }; |
| 132 | + let qubit = layer_index * 2; |
| 133 | + Ok(( |
| 134 | + PackedOperation::from_instruction(Box::new(instruction)), |
| 135 | + smallvec![Param::Obj(unitary.unbind().into())], |
| 136 | + vec![permutation[qubit], permutation[qubit + 1]], |
| 137 | + vec![], |
| 138 | + )) |
| 139 | + }; |
| 140 | + |
| 141 | + let mut per_thread = num_unitaries / UNITARY_PER_SEED; |
| 142 | + if per_thread == 0 { |
| 143 | + per_thread = 10; |
| 144 | + } |
| 145 | + let mut outer_rng = match seed { |
| 146 | + Some(seed) => Pcg64Mcg::seed_from_u64(seed), |
| 147 | + None => Pcg64Mcg::from_entropy(), |
| 148 | + }; |
| 149 | + let seed_vec: Vec<u64> = rand::distributions::Standard |
| 150 | + .sample_iter(&mut outer_rng) |
| 151 | + .take(num_unitaries) |
| 152 | + .collect(); |
| 153 | + |
| 154 | + let unitaries: Vec<Array2<Complex64>> = if getenv_use_multiple_threads() && num_unitaries > 200 |
| 155 | + { |
| 156 | + seed_vec |
| 157 | + .par_chunks(per_thread) |
| 158 | + .flat_map_iter(|seeds| random_unitaries(seeds[0], seeds.len())) |
| 159 | + .collect() |
| 160 | + } else { |
| 161 | + seed_vec |
| 162 | + .chunks(per_thread) |
| 163 | + .flat_map(|seeds| random_unitaries(seeds[0], seeds.len())) |
| 164 | + .collect() |
| 165 | + }; |
| 166 | + CircuitData::from_packed_operations( |
| 167 | + py, |
| 168 | + num_qubits, |
| 169 | + 0, |
| 170 | + unitaries |
| 171 | + .into_iter() |
| 172 | + .enumerate() |
| 173 | + .map(|x| build_instruction(x, &mut outer_rng)), |
| 174 | + Param::Float(0.), |
| 175 | + ) |
| 176 | +} |
0 commit comments