Skip to content

Commit

Permalink
Fix synth_mcmt_vchain for gates with parameters (Qiskit#13315)
Browse files Browse the repository at this point in the history
* Fix mcmt-vchain with params

* add reno

* remove reno

* add test that gates are 1q

* add test for 1q gates
  • Loading branch information
Cryoris authored Oct 15, 2024
1 parent efdcb75 commit fbfe738
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion crates/accelerate/src/synthesis/multi_controlled/mcmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn mcmt_v_chain(
}

let packed_controlled_gate = controlled_gate.operation;
let gate_params = controlled_gate.params;
let num_qubits = if num_ctrl_qubits > 1 {
2 * num_ctrl_qubits - 1 + num_target_qubits
} else {
Expand Down Expand Up @@ -135,7 +136,7 @@ pub fn mcmt_v_chain(
let targets = (0..num_target_qubits).map(|i| {
Ok((
packed_controlled_gate.clone(),
smallvec![] as SmallVec<[Param; 3]>,
gate_params.clone(),
vec![Qubit::new(master_control), Qubit::new(num_ctrl_qubits + i)],
vec![] as Vec<Clbit>,
))
Expand Down
3 changes: 3 additions & 0 deletions qiskit/synthesis/multi_controlled/mcmt_vchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def synth_mcmt_vchain(
└───┘ └───┘
"""
if gate.num_qubits != 1:
raise ValueError("Only single qubit gates are supported as input.")

circ = QuantumCircuit._from_circuit_data(
mcmt_v_chain(gate.control(), num_ctrl_qubits, num_target_qubits, ctrl_state)
)
Expand Down
21 changes: 20 additions & 1 deletion test/python/circuit/library/test_mcmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

from qiskit.exceptions import QiskitError
from qiskit.compiler import transpile
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.circuit import QuantumCircuit, QuantumRegister, Parameter
from qiskit.circuit.library import (
MCMT,
MCMTVChain,
CHGate,
XGate,
ZGate,
RYGate,
CXGate,
CZGate,
MCMTGate,
Expand Down Expand Up @@ -264,6 +265,24 @@ def test_invalid_base_gate_width(self):
with self.assertRaises(ValueError):
_ = MCMTGate(gate, 10, 2)

def test_invalid_base_gate_width_synthfun(self):
"""Test only 1-qubit base gates are accepted."""
for gate in [GlobalPhaseGate(0.2), SwapGate()]:
with self.subTest(gate=gate):
with self.assertRaises(ValueError):
_ = synth_mcmt_vchain(gate, 10, 2)

def test_gate_with_parameters_vchain(self):
"""Test a gate with parameters as base gate."""
theta = Parameter("th")
gate = RYGate(theta)
num_target = 3
circuit = synth_mcmt_vchain(gate, num_ctrl_qubits=10, num_target_qubits=num_target)

self.assertEqual(circuit.count_ops().get("cry", 0), num_target)
self.assertEqual(circuit.num_parameters, 1)
self.assertIs(circuit.parameters[0], theta)


if __name__ == "__main__":
unittest.main()

0 comments on commit fbfe738

Please sign in to comment.