Skip to content

Commit

Permalink
Add cnot gate to the models (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSerraPeralta authored Oct 18, 2024
1 parent ae692f7 commit 5ec4561
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
56 changes: 56 additions & 0 deletions surface_sim/models/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ def cphase(self, qubits: Sequence[str]) -> Circuit:
circ.append(CircuitInstruction("DEPOLARIZE2", ind_pair, [prob]))
return circ

def cnot(self, qubits: Sequence[str]) -> Circuit:
if len(qubits) % 2 != 0:
raise ValueError("Expected and even number of qubits.")

inds = self.get_inds(qubits)
circ = Circuit()

circ.append(CircuitInstruction("CNOT", inds))

for qubit_pair, ind_pair in zip(grouper(qubits, 2), grouper(inds, 2)):
prob = self.param("cz_error_prob", *qubit_pair)
circ.append(CircuitInstruction("DEPOLARIZE2", ind_pair, [prob]))
return circ

def measure(self, qubits: Iterable[str]) -> Circuit:
inds = self.get_inds(qubits)
circ = Circuit()
Expand Down Expand Up @@ -232,6 +246,26 @@ def cphase(self, qubits: Sequence[str]) -> Circuit:
circ.append(CircuitInstruction("PAULI_CHANNEL_2", ind_pair, probs))
return circ

def cnot(self, qubits: Sequence[str]) -> Circuit:
if len(qubits) % 2 != 0:
raise ValueError("Expected and even number of qubits.")

inds = self.get_inds(qubits)
circ = Circuit()

circ.append(CircuitInstruction("CNOT", inds))

for qubit_pair, ind_pair in zip(grouper(qubits, 2), grouper(inds, 2)):
prob = self.param("cz_error_prob", *qubit_pair)
prefactors = biased_prefactors(
biased_pauli=self.param("biased_pauli", *qubit_pair),
biased_factor=self.param("biased_factor", *qubit_pair),
num_qubits=2,
)
probs = prob * prefactors
circ.append(CircuitInstruction("PAULI_CHANNEL_2", ind_pair, probs))
return circ

def measure(self, qubits: Iterable[str]) -> Circuit:
inds = self.get_inds(qubits)
circ = Circuit()
Expand Down Expand Up @@ -339,6 +373,9 @@ def s_dag_gate(self, qubits: Iterable[str]) -> Circuit:
def cphase(self, qubits: Iterable[str]) -> Circuit:
return self.generic_op("CZ", qubits)

def cnot(self, qubits: Iterable[str]) -> Circuit:
return self.generic_op("CNOT", qubits)

def measure(self, qubits: Iterable[str]) -> Circuit:
circ = Circuit()
name = "M"
Expand Down Expand Up @@ -468,6 +505,20 @@ def cphase(self, qubits: Sequence[str]) -> Circuit:
circ.append(CircuitInstruction("DEPOLARIZE2", ind_pair, [prob]))
return circ

def cnot(self, qubits: Sequence[str]) -> Circuit:
if len(qubits) % 2 != 0:
raise ValueError("Expected and even number of qubits.")

inds = self.get_inds(qubits)
circ = Circuit()

circ.append(CircuitInstruction("CNOT", inds))

for qubit_pair, ind_pair in zip(grouper(qubits, 2), grouper(inds, 2)):
prob = self.param("cz_error_prob", *qubit_pair)
circ.append(CircuitInstruction("DEPOLARIZE2", ind_pair, [prob]))
return circ

def measure(self, qubits: Iterable[str]) -> Circuit:
inds = self.get_inds(qubits)
circ = Circuit()
Expand Down Expand Up @@ -573,6 +624,11 @@ def cphase(self, qubits: Sequence[str]) -> Circuit:
circ.append(CircuitInstruction("CZ", self.get_inds(qubits)))
return circ

def cnot(self, qubits: Sequence[str]) -> Circuit:
circ = Circuit()
circ.append(CircuitInstruction("CNOT", self.get_inds(qubits)))
return circ

def measure(self, qubits: Iterable[str]) -> Circuit:
circ = Circuit()
for qubit in qubits:
Expand Down
3 changes: 3 additions & 0 deletions surface_sim/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def s_gate(self, qubits: Iterable[str]) -> Circuit:
def s_dag_gate(self, qubits: Iterable[str]) -> Circuit:
raise NotImplementedError

def cnot(self, qubits: Sequence[str]) -> Circuit:
raise NotImplementedError

def cphase(self, qubits: Sequence[str]) -> Circuit:
raise NotImplementedError

Expand Down
31 changes: 31 additions & 0 deletions tests/models/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"Z": 1,
"H": 1,
"CZ": 1,
"CNOT": 1,
"M": 1,
"R": 1,
},
Expand Down Expand Up @@ -59,6 +60,12 @@ def test_NoiselessModel():
ops = [o.name for o in model.cphase(["D1", "D2"])]
assert ops == ["CZ"]

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert ops == ["CX"]

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert ops == ["CX"]

ops = [o.name for o in model.measure(["D1"])]
assert ops == ["M"]

Expand Down Expand Up @@ -87,6 +94,12 @@ def test_PhenomenologicalNoiseModel():
ops = [o.name for o in model.cphase(["D1", "D2"])]
assert ops == ["CZ"]

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert ops == ["CX"]

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert ops == ["CX"]

ops = [o.name for o in model.measure(["D1"])]
assert "M" in ops
assert set(NOISE_GATES + ["M"]) >= set(ops)
Expand Down Expand Up @@ -121,6 +134,9 @@ def test_IncomingNoiseModel():
ops = [o.name for o in model.cphase(["D1", "D2"])]
assert ops == ["CZ"]

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert ops == ["CX"]

ops = [o.name for o in model.measure(["D1"])]
assert ops == ["M"]

Expand Down Expand Up @@ -161,6 +177,11 @@ def test_DecoherentNoiseModel():
assert set(NOISE_GATES + ["CZ"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert "CX" in ops
assert set(NOISE_GATES + ["CX"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.measure(["D1"])]
assert "M" in ops
assert set(NOISE_GATES + ["M"]) >= set(ops)
Expand Down Expand Up @@ -205,6 +226,11 @@ def test_ExperimentalNoiseModel():
assert set(NOISE_GATES + ["CZ"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert "CX" in ops
assert set(NOISE_GATES + ["CX"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.measure(["D1"])]
assert "M" in ops
assert set(NOISE_GATES + ["M"]) >= set(ops)
Expand Down Expand Up @@ -249,6 +275,11 @@ def test_CircuitNoiseModel():
assert set(NOISE_GATES + ["CZ"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.cnot(["D1", "D2"])]
assert "CX" in ops
assert set(NOISE_GATES + ["CX"]) >= set(ops)
assert len(ops) > 1

ops = [o.name for o in model.measure(["D1"])]
assert "M" in ops
assert set(NOISE_GATES + ["M"]) >= set(ops)
Expand Down

0 comments on commit 5ec4561

Please sign in to comment.