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

Test gate copy perf #118

Merged
merged 10 commits into from
Jan 24, 2022
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
7 changes: 4 additions & 3 deletions tangelo/linq/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ def is_mixed_state(self):
"""
return "MEASURE" in self.counts

def add_gate(self, gate):
def add_gate(self, g):
"""Add a new gate to a circuit object and update other fields of the
circuit object to gradually keep track of its properties (gate count,
qubit indices...).
"""
# Add the gate to the list of gates
self._gates += [gate]
# Add a copy of the gate to the list of gates
gate = Gate(g.name, g.target, g.control, g.parameter, g.is_variational)
self._gates.append(gate)

# A circuit is variational as soon as a variational gate is added to it
if gate.is_variational:
Expand Down
15 changes: 15 additions & 0 deletions tangelo/linq/tests/test_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def test_is_variational(self):
self.assertTrue(circuit1.is_variational is False)
self.assertTrue(circuit3.is_variational is True)

def test_gate_data_is_copied(self):
""" Ensure that circuit is not referencing mutable variables that could cause it to change after
instantiation if the values of the variables are later changed in external code. """

mygates2 = copy.deepcopy(mygates)
c1 = Circuit(mygates2)

g = mygates2[0]
g.target.append(1)
g.name = 'POTATO'
g.parameter = -999.

c2 = Circuit(mygates2)
self.assertTrue(c1 != c2)

def test_width(self):
""" Ensure the width attribute of the circuit object (number of qubits) matches the gate operations
present in the circuit. """
Expand Down