Skip to content
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

Bug fix in conversion of PauliNoise to Qulacs #392

Merged
merged 11 commits into from
Jan 8, 2025
38 changes: 38 additions & 0 deletions packages/qulacs/tests/qulacs/circuit/test_noise_model_qulacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
KrausNoise,
MeasurementNoise,
NoiseModel,
PauliNoise,
PhaseFlipNoise,
)
from quri_parts.qulacs.circuit.noise import convert_circuit_with_noise_model
Expand Down Expand Up @@ -288,3 +289,40 @@ def test_convert_empty_circuit() -> None:
noise_model_readout = NoiseModel([MeasurementNoise([BitFlipNoise(1.0)])])
converted_readout = convert_circuit_with_noise_model(circuit, noise_model_readout)
assert converted_readout.get_gate_count() == 1


def test_convert_pauli_noise() -> None:
circuit = QuantumCircuit(3)
circuit.add_H_gate(2)
circuit.add_X_gate(0)
circuit.add_CNOT_gate(2, 1)
circuit.add_Z_gate(2)

noises = [
PauliNoise(
pauli_list=[[1, 2], [2, 3]],
prob_list=[0.001, 0.002],
qubit_indices=[1, 2],
target_gates=[names.CNOT],
)
]
model = NoiseModel(noises)

converted = convert_circuit_with_noise_model(circuit, model)
expected_gates = [
qulacs.gate.H(2),
qulacs.gate.X(0),
qulacs.gate.CNOT(2, 1),
qulacs.gate.Probabilistic(
[0.001, 0.002],
[
qulacs.gate.Pauli([1, 2], [1, 2]),
qulacs.gate.Pauli([1, 2], [2, 3]),
],
),
qulacs.gate.Z(2),
]

assert converted.get_gate_count() == len(expected_gates)
for i, expected_gate in enumerate(expected_gates):
assert converted.get_gate(i).to_json() == expected_gate.to_json()
24 changes: 15 additions & 9 deletions packages/rust/src/qulacs/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,36 @@ fn make_dense_matrix<'py>(

fn convert_add_pauli_noise<'py>(
py: Python<'py>,
qubits: &Vec<usize>,
_qubits: &Vec<usize>,
pauli_noise: &GateNoiseInstruction,
qulacs_circuit: Bound<'py, PyAny>,
fill_identity: bool,
) -> PyResult<Bound<'py, PyAny>> {
let module = py.import_bound("qulacs.gate")?;
let mut probs = pauli_noise.prob_list.clone();
let paulies = pauli_noise.pauli_list.clone();
let mut gates = vec![];
for pauli in &pauli_noise.pauli_list {
gates.push(
module
.getattr("Pauli")?
.call1((qubits.clone(), pauli.clone()))?,
.call1((pauli_noise.qubit_indices.clone(), pauli.clone()))?,
);
}
let psum: f64 = probs.iter().sum();
if psum < 1.0 {
gates.push(module.getattr("Identity")?.call1((qubits.len(),))?);
probs.push(1.0 - psum);
if fill_identity {
let psum: f64 = probs.iter().sum();
if psum < 1.0 {
gates.push(
module
.getattr("Identity")?
.call1((pauli_noise.qubit_indices[0],))?,
);
probs.push(1.0 - psum);
}
}
let prob_gate = py
.import_bound("qulacs.gate")?
.getattr("Probabilistic")?
.call1((probs, paulies))?;
.call1((probs, gates))?;
qulacs_circuit.call_method1("add_gate", (prob_gate,))?;
Ok(qulacs_circuit)
}
Expand Down Expand Up @@ -128,7 +134,7 @@ fn convert_add_noise<'py>(
)?;
}
"PauliNoise" | "GeneralDepolarizingNoise" => {
circuit = convert_add_pauli_noise(py, qubits, noise, circuit)?;
circuit = convert_add_pauli_noise(py, qubits, noise, circuit, false)?;
}
"ProbabilisticNoise" => {
circuit = convert_add_probabilistic_noise(py, qubits, noise, circuit)?;
Expand Down
Loading