Skip to content

Commit c666224

Browse files
trigpolynomCryoris
authored andcommitted
Fix behavior of n_local calling circuits for num_qubits=1 (#13523)
* added fix for efficient_su2 * making fix for real_amplitudes and excitation_preserving * formatted * passing tests * added release notes * fixed pauli_two_design * fixed format * fixed pauli_two_design issue * fixed unused gate * Update qiskit/circuit/library/n_local/pauli_two_design.py Co-authored-by: Julien Gacon <[email protected]> * addressing change --------- Co-authored-by: Julien Gacon <[email protected]> (cherry picked from commit 82bbcaa)
1 parent 7c2c8a1 commit c666224

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

qiskit/circuit/library/n_local/efficient_su2.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ def efficient_su2(
114114
if su2_gates is None:
115115
su2_gates = ["ry", "rz"]
116116

117+
# Set entanglement_blocks to None when num_qubits == 1
118+
entanglement_blocks = ["cx"] if num_qubits > 1 else []
119+
117120
return n_local(
118121
num_qubits,
119122
su2_gates,
120-
["cx"],
123+
entanglement_blocks,
121124
entanglement,
122125
reps,
123126
insert_barriers,

qiskit/circuit/library/n_local/excitation_preserving.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,21 @@ def excitation_preserving(
112112
raise ValueError(f"Unsupported mode {mode}, choose one of {supported_modes}")
113113

114114
theta = Parameter("θ")
115-
swap = QuantumCircuit(2, name="Interaction")
116-
swap.rxx(theta, 0, 1)
117-
swap.ryy(theta, 0, 1)
118-
if mode == "fsim":
119-
phi = Parameter("φ")
120-
swap.cp(phi, 0, 1)
115+
if num_qubits > 1:
116+
swap = QuantumCircuit(2, name="Interaction")
117+
swap.rxx(theta, 0, 1)
118+
swap.ryy(theta, 0, 1)
119+
if mode == "fsim":
120+
phi = Parameter("φ")
121+
swap.cp(phi, 0, 1)
122+
entanglement_blocks = [swap.to_gate()]
123+
else:
124+
entanglement_blocks = []
121125

122126
return n_local(
123127
num_qubits,
124128
["rz"],
125-
[swap.to_gate()],
129+
entanglement_blocks,
126130
entanglement,
127131
reps,
128132
insert_barriers,

qiskit/circuit/library/n_local/pauli_two_design.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@ def pauli_two_design(
8888
"""
8989
rng = np.random.default_rng(seed)
9090
random_block = Block.from_callable(1, 1, lambda params: _random_pauli_builder(params, rng))
91-
cz_block = Block.from_standard_gate(CZGate._standard_gate)
91+
entanglement_block = [Block.from_standard_gate(CZGate._standard_gate)] if num_qubits > 1 else []
9292

9393
data = py_n_local(
9494
num_qubits=num_qubits,
9595
reps=reps,
9696
rotation_blocks=[random_block],
97-
entanglement_blocks=[cz_block],
97+
entanglement_blocks=entanglement_block,
9898
entanglement=["pairwise"],
9999
insert_barriers=insert_barriers,
100100
skip_final_rotation_layer=False,
101101
skip_unentangled_qubits=False,
102102
parameter_prefix=parameter_prefix,
103103
)
104+
104105
two_design = QuantumCircuit._from_circuit_data(data)
105106

106107
circuit = QuantumCircuit(num_qubits, name=name)

qiskit/circuit/library/n_local/real_amplitudes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ def real_amplitudes(
114114
Returns:
115115
A real-amplitudes circuit.
116116
"""
117+
# Set entanglement_blocks to None when num_qubits == 1
118+
entanglement_blocks = ["cx"] if num_qubits > 1 else []
117119

118120
return n_local(
119121
num_qubits,
120122
["ry"],
121-
["cx"],
123+
entanglement_blocks,
122124
entanglement,
123125
reps,
124126
insert_barriers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fixes:
2+
- |
3+
Fixed a bug that caused the circuit library functions :func:`.efficient_su2`,
4+
:func:`.real_amplitudes`, :func:`.excitation_preserving` and :func:`.pauli_two_design`
5+
to error out when constructed for ``num_qubits==1``. For a single qubit these
6+
circuits will not contain any 2-qubit gates.

test/python/circuit/library/test_nlocal.py

+29
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,24 @@ def test_real_amplitudes(self):
784784
expected = n_local(4, "ry", "cx", "reverse_linear", reps=3)
785785
self.assertEqual(expected.assign_parameters(circuit.parameters), circuit)
786786

787+
def test_real_amplitudes_numqubits_equal1(self):
788+
"""Test the real amplitudes circuit for a single qubit."""
789+
circuit = real_amplitudes(1)
790+
expected = n_local(1, "ry", [])
791+
self.assertEqual(expected.assign_parameters(circuit.parameters), circuit)
792+
787793
def test_efficient_su2(self):
788794
"""Test the efficient SU(2) circuit."""
789795
circuit = efficient_su2(4)
790796
expected = n_local(4, ["ry", "rz"], "cx", "reverse_linear", reps=3)
791797
self.assertEqual(expected.assign_parameters(circuit.parameters), circuit)
792798

799+
def test_efficient_su2_numqubits_equal1(self):
800+
"""Test the efficient SU(2) circuit for a single qubit."""
801+
circuit = efficient_su2(1)
802+
expected = n_local(1, ["ry", "rz"], [])
803+
self.assertEqual(expected.assign_parameters(circuit.parameters), circuit)
804+
793805
@data("fsim", "iswap")
794806
def test_excitation_preserving(self, mode):
795807
"""Test the excitation preserving circuit."""
@@ -808,6 +820,15 @@ def test_excitation_preserving(self, mode):
808820
expected.assign_parameters(circuit.parameters).decompose(), circuit.decompose()
809821
)
810822

823+
@data("fsim", "iswap")
824+
def test_excitation_preserving_numqubits_equal1(self, mode):
825+
"""Test the excitation preserving circuit for a single qubit."""
826+
circuit = excitation_preserving(1, mode=mode)
827+
expected = n_local(1, "rz", [])
828+
self.assertEqual(
829+
expected.assign_parameters(circuit.parameters).decompose(), circuit.decompose()
830+
)
831+
811832
def test_excitation_preserving_invalid_mode(self):
812833
"""Test an error is raised for an invalid mode."""
813834
with self.assertRaises(ValueError):
@@ -824,6 +845,14 @@ def test_two_design(self):
824845

825846
self.assertTrue(circuit_ops.issubset(expected_ops))
826847

848+
def test_two_design_numqubits_equal1(self):
849+
"""Test the Pauli 2-design circuit for a single qubit."""
850+
circuit = pauli_two_design(1)
851+
expected_ops = {"rx", "ry", "rz", "id"}
852+
circuit_ops = set(circuit.count_ops().keys())
853+
854+
self.assertTrue(circuit_ops.issubset(expected_ops))
855+
827856
def test_two_design_seed(self):
828857
"""Test the seed"""
829858
seed1 = 123

0 commit comments

Comments
 (0)