From 0f72155e89cecea4c43b115fe0dffed900ee61fb Mon Sep 17 00:00:00 2001 From: ValentinS4t1qbit <41597680+ValentinS4t1qbit@users.noreply.github.com> Date: Sun, 23 Jan 2022 13:29:42 -0800 Subject: [PATCH 1/9] Test gate copy perf --- tangelo/linq/circuit.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index 895acb901..26e3a277b 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -134,7 +134,13 @@ def add_gate(self, gate): qubit indices...). """ # Add the gate to the list of gates - self._gates += [gate] + #self._gates += [gate] + + self._gates += [type('Gate', (), gate.__dict__.copy())] + #print(id(gate), id(self._gates[-1])) + + # self._gates += [Gate(gate.name, list(gate.target).copy(), list(gate.control).copy(), + # gate.parameter, gate.is_variational)] # A circuit is variational as soon as a variational gate is added to it if gate.is_variational: From 4f096e007829f1cf8ab2784ddda46800585a487c Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Sun, 23 Jan 2022 22:00:09 -0800 Subject: [PATCH 2/9] deepcopy gate for ref7 --- tangelo/linq/circuit.py | 31 +++++++++++++++++++++++++------ tangelo/linq/gate.py | 8 ++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index 26e3a277b..941b3886c 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -134,17 +134,32 @@ def add_gate(self, gate): qubit indices...). """ # Add the gate to the list of gates - #self._gates += [gate] - - self._gates += [type('Gate', (), gate.__dict__.copy())] + g = copy.deepcopy(gate) + self._gates.append(g) + + # g = Gate(gate.name, target=gate.target.copy()) + # for key, value in gate.__dict__.copy().items(): + # setattr(g, key, value) + # self._gates.append(g) + # + # print(id(gate.name), id(g.name)) + # + # print(gate) + # print(g) + # + # gate.target = [5] + # print(gate) + # print(g) + + #self._gates += [type(Gate, (), gate.__dict__.copy())] #print(id(gate), id(self._gates[-1])) # self._gates += [Gate(gate.name, list(gate.target).copy(), list(gate.control).copy(), # gate.parameter, gate.is_variational)] # A circuit is variational as soon as a variational gate is added to it - if gate.is_variational: - self._variational_gates.append(gate) + if g.is_variational: + self._variational_gates.append(g) def check_index_valid(index): """If circuit size was specified at instantiation, check that qubit @@ -253,7 +268,11 @@ def inverse(self): Returns: Circuit: the inverted circuit """ - gates = [gate.inverse() for gate in reversed(self._gates)] + #gates = [gate.inverse() for gate in reversed(self._gates)] + gates = [] + for gate in reversed(self._gates): + g = gate.inverse() + gates.append(g) return Circuit(gates, n_qubits=self.width) def serialize(self): diff --git a/tangelo/linq/gate.py b/tangelo/linq/gate.py index 865e928f7..95333f70a 100644 --- a/tangelo/linq/gate.py +++ b/tangelo/linq/gate.py @@ -111,6 +111,14 @@ def __ne__(self, other): """Define inequality (!=) operator on gates""" return not (self == other) + # @classmethod + # def from_dict(cls, **kwargs): + # """Build a new Gate object from the underlying dictionary""" + # + # Gate + # for key, value in kwargs.items(): + # setattr(self, key, value) + def inverse(self): """Return the inverse (adjoint) of a gate. From 645302463093ad253b227c1ff3965d6eafcd5119 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Sun, 23 Jan 2022 22:03:53 -0800 Subject: [PATCH 3/9] deepcopy gate for ref7 --- tangelo/linq/circuit.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index 941b3886c..bc4e0cb6a 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -128,14 +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 - g = copy.deepcopy(gate) - self._gates.append(g) + gate = copy.deepcopy(g) + self._gates.append(gate) # g = Gate(gate.name, target=gate.target.copy()) # for key, value in gate.__dict__.copy().items(): @@ -158,8 +158,8 @@ def add_gate(self, gate): # gate.parameter, gate.is_variational)] # A circuit is variational as soon as a variational gate is added to it - if g.is_variational: - self._variational_gates.append(g) + if gate.is_variational: + self._variational_gates.append(gate) def check_index_valid(index): """If circuit size was specified at instantiation, check that qubit From 34fb3abb602a1d19ec2d70f6dc62289ec5bc4d76 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Sun, 23 Jan 2022 22:24:09 -0800 Subject: [PATCH 4/9] deepcopy gate for ref7 --- tangelo/linq/circuit.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index bc4e0cb6a..e8aa93cd0 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -134,28 +134,16 @@ def add_gate(self, g): qubit indices...). """ # Add the gate to the list of gates - gate = copy.deepcopy(g) + #gate = copy.deepcopy(g) + gate = Gate(g.name, g.target, g.control, g.parameter, g.is_variational) self._gates.append(gate) - # g = Gate(gate.name, target=gate.target.copy()) - # for key, value in gate.__dict__.copy().items(): - # setattr(g, key, value) - # self._gates.append(g) - # # print(id(gate.name), id(g.name)) - # - # print(gate) - # print(g) - # - # gate.target = [5] - # print(gate) - # print(g) - - #self._gates += [type(Gate, (), gate.__dict__.copy())] - #print(id(gate), id(self._gates[-1])) - - # self._gates += [Gate(gate.name, list(gate.target).copy(), list(gate.control).copy(), - # gate.parameter, gate.is_variational)] + # print(id(gate.target), id(g.target)) + # print(id(gate.control), id(g.control)) + # print(id(gate.parameter), id(g.parameter)) + # print(id(gate.is_variational), id(g.is_variational)) + # print('\n') # A circuit is variational as soon as a variational gate is added to it if gate.is_variational: From a85ce457707dc7e5cb70dabf25e397f9c0fd902e Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Sun, 23 Jan 2022 22:48:37 -0800 Subject: [PATCH 5/9] solution for copy of gate. Very small impact on time for running tests overall --- tangelo/linq/circuit.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index e8aa93cd0..532b4d560 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -133,18 +133,10 @@ def add_gate(self, g): circuit object to gradually keep track of its properties (gate count, qubit indices...). """ - # Add the gate to the list of gates - #gate = copy.deepcopy(g) + # 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) - # print(id(gate.name), id(g.name)) - # print(id(gate.target), id(g.target)) - # print(id(gate.control), id(g.control)) - # print(id(gate.parameter), id(g.parameter)) - # print(id(gate.is_variational), id(g.is_variational)) - # print('\n') - # A circuit is variational as soon as a variational gate is added to it if gate.is_variational: self._variational_gates.append(gate) From 2b14aad6003df72871bb72cba19e0eb38380fa16 Mon Sep 17 00:00:00 2001 From: ValentinS4t1qbit <41597680+ValentinS4t1qbit@users.noreply.github.com> Date: Sun, 23 Jan 2022 23:08:12 -0800 Subject: [PATCH 6/9] Update circuit.py --- tangelo/linq/circuit.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index 532b4d560..dad128735 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -248,11 +248,7 @@ def inverse(self): Returns: Circuit: the inverted circuit """ - #gates = [gate.inverse() for gate in reversed(self._gates)] - gates = [] - for gate in reversed(self._gates): - g = gate.inverse() - gates.append(g) + gates = [gate.inverse() for gate in reversed(self._gates)] return Circuit(gates, n_qubits=self.width) def serialize(self): From e277c6db7a7b66cfe77215ec7341d7e997c9b184 Mon Sep 17 00:00:00 2001 From: ValentinS4t1qbit <41597680+ValentinS4t1qbit@users.noreply.github.com> Date: Sun, 23 Jan 2022 23:14:52 -0800 Subject: [PATCH 7/9] Update gate.py --- tangelo/linq/gate.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tangelo/linq/gate.py b/tangelo/linq/gate.py index 95333f70a..865e928f7 100644 --- a/tangelo/linq/gate.py +++ b/tangelo/linq/gate.py @@ -111,14 +111,6 @@ def __ne__(self, other): """Define inequality (!=) operator on gates""" return not (self == other) - # @classmethod - # def from_dict(cls, **kwargs): - # """Build a new Gate object from the underlying dictionary""" - # - # Gate - # for key, value in kwargs.items(): - # setattr(self, key, value) - def inverse(self): """Return the inverse (adjoint) of a gate. From 82e4afd249a7da0a505f0f97e2cfe81b387f9c2e Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Sun, 23 Jan 2022 23:33:22 -0800 Subject: [PATCH 8/9] Added test --- tangelo/linq/tests/test_circuits.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tangelo/linq/tests/test_circuits.py b/tangelo/linq/tests/test_circuits.py index 708e25795..606d23ba6 100644 --- a/tangelo/linq/tests/test_circuits.py +++ b/tangelo/linq/tests/test_circuits.py @@ -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 the circuit is not referencing mutable variables that could cause it to change after + instantiation if the values of the variable are changed by the user """ + + 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. """ From 5dca30822a1b0142742f6faeb8f120209d23c3a7 Mon Sep 17 00:00:00 2001 From: ValentinS4t1qbit <41597680+ValentinS4t1qbit@users.noreply.github.com> Date: Mon, 24 Jan 2022 11:07:21 -0800 Subject: [PATCH 9/9] Update test_circuits.py --- tangelo/linq/tests/test_circuits.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tangelo/linq/tests/test_circuits.py b/tangelo/linq/tests/test_circuits.py index 606d23ba6..591fbdfdf 100644 --- a/tangelo/linq/tests/test_circuits.py +++ b/tangelo/linq/tests/test_circuits.py @@ -60,8 +60,8 @@ def test_is_variational(self): self.assertTrue(circuit3.is_variational is True) def test_gate_data_is_copied(self): - """ Ensure that the circuit is not referencing mutable variables that could cause it to change after - instantiation if the values of the variable are changed by the user """ + """ 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)