8000 Implemented a type check in copy method of QuantumCircuit class by Abhiraj-Shrotriya · Pull Request #10521 · Qiskit/qiskit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implemented a type check in copy method of QuantumCircuit class #10521

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

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ def copy(self, name: str | None = None) -> "QuantumCircuit":
"""Copy the circuit.

Args:
name (str): name to be given to the copied circuit. If None, then the name stays the same
name (str): name to be given to the copied circuit. If None, then the name stays the same.

Returns:
QuantumCircuit: a deepcopy of the current circuit, with the specified name
Expand Down Expand Up @@ -2222,6 +2222,10 @@ def copy_empty_like(self, name: str | None = None) -> "QuantumCircuit":
Returns:
QuantumCircuit: An empty copy of self.
"""
if not (name is None or isinstance(name, str)):
raise TypeError(
f"invalid name for a circuit: '{name}'. The name must be a string or 'None'."
)
cpy = copy.copy(self)
# copy registers correctly, in copy.copy they are only copied via reference
cpy.qregs = self.qregs.copy()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
The methods :meth:`.QuantumCircuit.copy` and :meth:`~.QuantumCircuit.copy_empty_like` will now raise an
error if the ``name`` argument is incorrectly typed, instead of generating an invalid circuit.
16 changes: 16 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ def test_copy_empty_like_circuit(self):
copied = qc.copy_empty_like("copy")
self.assertEqual(copied.name, "copy")

def test_circuit_copy_rejects_invalid_types(self):
"""Test copy method rejects argument with type other than 'string' and 'None' type."""
qc = QuantumCircuit(1, 1)
qc.h(0)

with self.assertRaises(TypeError):
qc.copy([1, "2", 3])

def test_circuit_copy_empty_like_rejects_invalid_types(self):
"""Test copy_empty_like method rejects argument with type other than 'string' and 'None' type."""
qc = QuantumCircuit(1, 1)
qc.h(0)

with self.assertRaises(TypeError):
qc.copy_empty_like(123)

def test_clear_circuit(self):
"""Test clear method deletes instructions in circuit."""
qr = QuantumRegister(2)
Expand Down
0