def test_chain_merge_prev(self, d, lib): b = bases.general(d) rng = np.random.RandomState(4242) dm = rng.randn(d*d, d*d) + 1j * rng.randn(d*d, d*d) dm += dm.conjugate().transpose() dm /= dm.trace() chain = Operation.from_sequence( (lib.cphase(angle=np.pi/7, leakage=0.25) if d == 3 else lib.cphase(3*np.pi / 7)).at(0, 1), lib.rotate_x(4 * np.pi / 7).at(0), ) bases_full = (b, b) chain_c = chain.compile(bases_full, bases_full) assert len(chain.operations) == 2 assert isinstance(chain_c, _PTMOperation) state1 = State.from_dm(dm, bases_full) state2 = State.from_dm(dm, bases_full) chain(state1, 0, 1) chain_c(state2, 0, 1) assert np.allclose(state1.meas_prob(0), state2.meas_prob(0)) assert np.allclose(state1.meas_prob(1), state2.meas_prob(1))
def test_chain_compile_leaking(self): b = bases.general(3) chain0 = Operation.from_sequence( lib3.rotate_x(0.5 * np.pi).at(2), lib3.cphase(leakage_rate=0.1).at(0, 2), lib3.cphase(leakage_rate=0.1).at(1, 2), lib3.rotate_x(-0.75 * np.pi).at(2), lib3.rotate_x(0.25 * np.pi).at(2), ) b0 = b.subbasis([0]) b01 = b.subbasis([0, 1]) b0134 = b.subbasis([0, 1, 3, 4]) chain1 = chain0.compile((b0, b0, b0134), (b, b, b)) assert isinstance(chain1, _Chain) # Ancilla is not leaking here anc_basis = chain1._units[1].operation.bases_out[1] for label in anc_basis.labels: assert '2' not in label chain2 = chain0.compile((b01, b01, b0134), (b, b, b)) # Ancilla is leaking here assert isinstance(chain2, _Chain) anc_basis = chain2._units[1].operation.bases_out[1] for label in '2', 'X20', 'Y20', 'X21', 'Y21': assert label in anc_basis.labels
def test_chain_merge_prev(self, d, lib): b = bases.general(d) rng = np.random.RandomState(4242) dm = rng.randn(d * d, d * d) + 1j * rng.randn(d * d, d * d) dm += dm.conjugate().transpose() dm /= dm.trace() chain = Operation.from_sequence( (lib.cphase(angle=np.pi / 7, leakage_rate=0.25) if d == 3 else lib.cphase(3 * np.pi / 7)).at(0, 1), lib.rotate_x(4 * np.pi / 7).at(0), ) bases_full = (b, b) chain_c = chain.compile(bases_full, bases_full) assert len(chain._units) == 2 assert isinstance(chain_c, PTMOperation) pv1 = PauliVector.from_dm(dm, bases_full) pv2 = PauliVector.from_dm(dm, bases_full) chain(pv1, 0, 1) chain_c(pv2, 0, 1) assert np.allclose(pv1.meas_prob(0), pv2.meas_prob(0)) assert np.allclose(pv1.meas_prob(1), pv2.meas_prob(1))
def test_create_untimed_model(): basis = (bases.general(2), ) class SampleModel(Model): dim = 2 @Model.gate() def rotate_y(self, qubit): return (ParametrizedOperation(lib.rotate_y, basis), ) @Model.gate() def cphase(self, qubit_static, qubit_fluxed): return (lib.cphase(pi).at(qubit_static, qubit_fluxed), ) sample_setup = Setup(""" setup: [] """) m = SampleModel(sample_setup) cnot = m.rotate_y('D0', angle=0.5*pi) + m.cphase('D0', 'D1') + \ m.rotate_y('D0', angle=-0.5*pi) assert cnot.finalize().operation.ptm(basis * 2, basis * 2) == approx( Operation.from_sequence( lib.rotate_y(0.5 * pi).at(0), lib.cphase(pi).at(0, 1), lib.rotate_y(-0.5 * pi).at(0), ).ptm(basis * 2, basis * 2))
def test_create_timed_model(): basis = (bases.general(2), ) class SampleModel(Model): dim = 2 @Model.gate(duration=20) def rotate_y(self, qubit): return ( self.wait(qubit, 10), ParametrizedOperation(lib.rotate_y, basis), self.wait(qubit, 10), ) @Model.gate(duration='t_twoqubit') def cphase(self, qubit_static, qubit_fluxed): return ( self.wait(qubit_static, 0.5 * self.p('t_twoqubit')), self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')), lib.cphase(pi).at(qubit_static, qubit_fluxed), self.wait(qubit_static, 0.5 * self.p('t_twoqubit')), self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')), ) @Model.gate(duration=lambda qubit, setup: 600 if qubit == 'D0' else 400) def strange_duration_gate(self, qubit): return lib.rotate_y(pi) @staticmethod def _filter_wait_placeholders(operation): return Operation.from_sequence([ unit for unit in operation.units() if not isinstance(unit.operation, WaitPlaceholder) ]) def finalize(self, circuit, bases_in=None): return circuit.finalize([self._filter_wait_placeholders], bases_in) sample_setup = Setup(""" setup: - t_twoqubit: 40 """) m = SampleModel(sample_setup) cnot = m.rotate_y('D0', angle=0.5*pi) + m.cphase('D0', 'D1') + \ m.rotate_y('D0', angle=-0.5*pi) cnot = m.finalize(cnot) assert cnot.operation.ptm(basis * 2, basis * 2) == approx( Operation.from_sequence( lib.rotate_y(0.5 * pi).at(0), lib.cphase(pi).at(0, 1), lib.rotate_y(-0.5 * pi).at(0), ).ptm(basis * 2, basis * 2)) gate1 = m.strange_duration_gate('D0') assert gate1.duration == 600 gate2 = m.strange_duration_gate('D1') assert gate2.duration == 400
def test_circuits_params(self): dim = 2 basis = (bases.general(dim), ) * 2 orotate = lib.rotate_y ocphase = lib.cphase grotate = Gate('Q0', dim, ParametrizedOperation(orotate, basis[:1])) gcphase = Gate(('Q0', 'Q1'), dim, ParametrizedOperation(ocphase, basis)) with pytest.raises(RuntimeError, match=r".*free parameters.*\n" r".*angle.*\n" r".*allow_param_repeat.*"): _ = gcphase + grotate with allow_param_repeat(): circuit = grotate + gcphase + grotate assert circuit.free_parameters == {sympy.symbols('angle')} assert len(circuit.gates) == 3 angle = 0.736 assert c_op(circuit(angle=angle)).ptm(basis, basis) == \ approx(Operation.from_sequence( orotate(angle).at(0), ocphase(angle).at(0, 1), orotate(angle).at(0) ).ptm(basis, basis)) angle1 = 0.4 * pi angle2 = 1.01 * pi angle3 = -0.6 * pi ptm_ref = Operation.from_sequence( orotate(angle1).at(0), ocphase(angle2).at(0, 1), orotate(angle3).at(0)).ptm(basis, basis) circuit = grotate(angle=angle1) + gcphase(angle=angle2) + \ grotate(angle=angle3) assert circuit.finalize().operation.ptm(basis, basis) == \ approx(ptm_ref) circuit = grotate(angle='angle1') + gcphase(angle='angle2') + \ grotate(angle='angle3') assert c_op(circuit(angle1=angle1, angle2=angle2, angle3=angle3)).ptm(basis, basis) == approx(ptm_ref)
def test_chain_compile_single_qubit(self, d, lib): b = bases.general(d) dm = random_hermitian_matrix(d, seed=487) bases_full = (b, ) subbases = (b.subbasis([0, 1]), ) angle = np.pi / 5 rx_angle = lib.rotate_x(angle) rx_2angle = lib.rotate_x(2 * angle) chain0 = Operation.from_sequence(rx_angle.at(0), rx_angle.at(0)) pv0 = PauliVector.from_dm(dm, bases_full) chain0(pv0, 0) assert chain0.num_qubits == 1 assert len(chain0._units) == 2 chain0_c = chain0.compile(bases_full, bases_full) assert isinstance(chain0_c, PTMOperation) pv1 = PauliVector.from_dm(dm, bases_full) chain0_c(pv1, 0) assert chain0_c.num_qubits == 1 assert isinstance(chain0_c, PTMOperation) op_angle = chain0_c op_2angle = rx_2angle.compile(bases_full, bases_full) assert isinstance(op_2angle, PTMOperation) assert op_angle.shape == op_2angle.shape assert op_angle.bases_in == op_2angle.bases_in assert op_angle.bases_out == op_2angle.bases_out assert op_angle.ptm(op_angle.bases_in, op_angle.bases_out) == \ approx(op_2angle.ptm(op_2angle.bases_in, op_2angle.bases_out)) assert pv1.to_pv() == approx(pv0.to_pv()) rx_pi = lib.rotate_x(np.pi) chain_2pi = Operation.from_sequence(rx_pi.at(0), rx_pi.at(0)) chain_2pi_c1 = chain_2pi.compile(subbases, bases_full) assert isinstance(chain_2pi_c1, PTMOperation) assert chain_2pi_c1.bases_in == subbases assert chain_2pi_c1.bases_out == subbases chain_2pi_c2 = chain_2pi.compile(bases_full, subbases) assert isinstance(chain_2pi_c2, PTMOperation) assert chain_2pi_c2.bases_in == subbases assert chain_2pi_c2.bases_out == subbases
def test_chain_compile_single_qubit(self, d, lib): b = bases.general(d) dm = random_density_matrix(d, seed=487) bases_full = (b,) subbases = (b.subbasis([0, 1]),) angle = np.pi/5 rx_angle = lib.rotate_x(angle) rx_2angle = lib.rotate_x(2*angle) chain0 = Operation.from_sequence(rx_angle.at(0), rx_angle.at(0)) state0 = State.from_dm(dm, bases_full) chain0(state0, 0) assert chain0.num_qubits == 1 assert len(chain0.operations) == 2 chain0_c = chain0.compile(bases_full, bases_full) state1 = State.from_dm(dm, bases_full) chain0_c(state1, 0) assert chain0_c.num_qubits == 1 assert isinstance(chain0_c, _PTMOperation) op_angle = chain0_c op_2angle = rx_2angle.compile(bases_full, bases_full) assert op_angle.shape == op_2angle.shape assert op_angle.bases_in == op_2angle.bases_in assert op_angle.bases_out == op_2angle.bases_out assert op_angle.ptm == approx(op_2angle.ptm) assert state1.to_pv() == approx(state0.to_pv()) rx_pi = lib.rotate_x(np.pi) chain_2pi = Operation.from_sequence(rx_pi.at(0), rx_pi.at(0)) chain_2pi_c1 = chain_2pi.compile(subbases, bases_full) assert isinstance(chain_2pi_c1, _PTMOperation) assert chain_2pi_c1.bases_in == subbases assert chain_2pi_c1.bases_out == subbases chain_2pi_c2 = chain_2pi.compile(bases_full, subbases) assert isinstance(chain_2pi_c2, _PTMOperation) assert chain_2pi_c2.bases_in == subbases assert chain_2pi_c2.bases_out == subbases
def test_circuits_add(self): dim = 2 orplus = lib.rotate_y(0.5 * pi) ocphase = lib.cphase(pi) orminus = lib.rotate_y(-0.5 * pi) grplus = Gate('Q0', dim, orplus) gcphase = Gate(('Q0', 'Q1'), dim, ocphase) grminus = Gate('Q0', dim, orminus) basis = (bases.general(2), ) * 2 circuit = grplus + gcphase assert circuit.qubits == ['Q0', 'Q1'] assert len(circuit.gates) == 2 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(0, 1)).ptm(basis, basis)) circuit = circuit + grminus assert circuit.qubits == ['Q0', 'Q1'] assert len(circuit.gates) == 3 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(0, 1), orminus.at(0)).ptm(basis, basis)) circuit = grplus + (gcphase + grminus) assert circuit.qubits == ['Q0', 'Q1'] assert len(circuit.gates) == 3 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(0, 1), orminus.at(0)).ptm(basis, basis)) grplus = Gate('Q1', dim, orplus) grminus = Gate('Q1', dim, orminus) circuit = grplus + gcphase + grminus assert circuit.qubits == ['Q1', 'Q0'] assert len(circuit.gates) == 3 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(0, 1), orminus.at(0)).ptm(basis, basis)) basis = (basis[0], ) * 3 grplus = Gate('Q2', dim, orplus) grminus = Gate('Q0', dim, orminus) circuit = grplus + gcphase + grminus assert circuit.qubits == ['Q2', 'Q0', 'Q1'] assert len(circuit.gates) == 3 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(1, 2), orminus.at(1)).ptm(basis, basis)) grplus = Gate('Q0', dim, orplus) grminus = Gate('Q2', dim, orminus) circuit = grplus + gcphase + grminus assert circuit.qubits == ['Q0', 'Q1', 'Q2'] assert len(circuit.gates) == 3 assert c_op(circuit).ptm(basis, basis) == approx( Operation.from_sequence(orplus.at(0), ocphase.at(0, 1), orminus.at(2)).ptm(basis, basis))
def test_compilation_with_placeholders(self): b_full = bases.general(3) b0 = b_full.subbasis([0]) b01 = b_full.subbasis([0, 1]) b012 = b_full.subbasis([0, 1, 2]) bases_in = (b01, b01, b0) bases_out = (b_full, b_full, b012) zz = Operation.from_sequence( lib3.rotate_x(-np.pi / 2).at(2), lib3.cphase(leakage_rate=0.1).at(0, 2), lib3.cphase(leakage_rate=0.25).at(2, 1), lib3.rotate_x(np.pi / 2).at(2), lib3.rotate_x(np.pi).at(0), lib3.rotate_x(np.pi).at(1)).compile(bases_in, bases_out) ptm_ref = zz.ptm(bases_in, bases_out) zz_parametrized = Operation.from_sequence( Operation.from_sequence( ParametrizedOperation(lambda angle1: lib3.rotate_x(angle1), (b_full, )).at(2), ParametrizedOperation( lambda lr02: lib3.cphase(leakage_rate=lr02), (b_full, ) * 2).at(0, 2), ParametrizedOperation( lambda lr21: lib3.cphase(leakage_rate=lr21), (b_full, ) * 2).at(2, 1), ParametrizedOperation(lambda angle2: lib3.rotate_x(angle2), (b_full, )).at(2), lib3.rotate_x(np.pi).at(0), lib3.rotate_x(np.pi).at(1))) zzpc = zz_parametrized.compile(bases_in, bases_out) assert isinstance(zzpc, _Chain) assert len(list(zzpc.units())) == 6 zz_parametrized = Operation.from_sequence( Operation.from_sequence( ParametrizedOperation(lambda angle1: lib3.rotate_x(angle1), (b_full, )).at(2), ParametrizedOperation( lambda lr02: lib3.cphase(leakage_rate=lr02), (b_full, ) * 2).at(0, 2), lib3.cphase(leakage_rate=0.25).at(2, 1), lib3.rotate_x(np.pi / 2).at(2), lib3.rotate_x(np.pi).at(0), lib3.rotate_x(np.pi).at(1))) zzpc = zz_parametrized.compile(bases_in, bases_out) assert len(list(zzpc.units())) == 4 params = dict(angle1=-np.pi / 2, lr02=0.1, foo='bar') new_units = [(op.substitute( **params) if isinstance(op, ParametrizedOperation) else op).at(*ix) for op, ix in zzpc.units()] zzpc = Operation.from_sequence(new_units).compile(bases_in, bases_out) assert len(zzpc._units) == 2 assert zzpc.ptm(bases_in, bases_out) == approx(ptm_ref)
def test_zz_parity_compilation(self): b_full = bases.general(3) b0 = b_full.subbasis([0]) b01 = b_full.subbasis([0, 1]) b012 = b_full.subbasis([0, 1, 2]) bases_in = (b01, b01, b0) bases_out = (b_full, b_full, b012) zz = Operation.from_sequence( lib3.rotate_x(-np.pi / 2).at(2), lib3.cphase(leakage_rate=0.1).at(0, 2), lib3.cphase(leakage_rate=0.25).at(2, 1), lib3.rotate_x(np.pi / 2).at(2), lib3.rotate_x(np.pi).at(0), lib3.rotate_x(np.pi).at(1)) zz_ptm = zz.ptm(bases_in, bases_out) zzc = zz.compile(bases_in=bases_in, bases_out=bases_out) zzc_ptm = zzc.ptm(bases_in, bases_out) assert zz_ptm == approx(zzc_ptm) units = list(zzc.units()) assert len(units) == 2 op1, ix1 = units[0] op2, ix2 = units[1] assert ix1 == (0, 2) assert ix2 == (1, 2) assert op1.bases_in[0] == bases_in[0] assert op2.bases_in[0] == bases_in[1] assert op1.bases_in[1] == bases_in[2] # Qubit 0 did not leak assert op1.bases_out[0] == bases_out[0].subbasis([0, 1]) # Qubit 1 leaked assert op2.bases_out[0] == bases_out[1].subbasis([0, 1, 2, 6]) # Qubit 2 is measured assert op2.bases_out[1] == bases_out[2] dm = random_hermitian_matrix(3**3, seed=85) pv1 = PauliVector.from_dm(dm, (b01, b01, b0)) pv2 = PauliVector.from_dm(dm, (b01, b01, b0)) zz(pv1, 0, 1, 2) zzc(pv2, 0, 1, 2) # Compiled version still needs to be projected, so we can't compare # Pauli vectors, so we can to check only DM diagonals. assert np.allclose(pv1.diagonal(), pv2.diagonal())
def test_zz_parity_compilation(self): b_full = bases.general(3) b0 = b_full.subbasis([0]) b01 = b_full.subbasis([0, 1]) b012 = b_full.subbasis([0, 1, 2]) bases_in = (b01, b01, b0) bases_out = (b_full, b_full, b012) zz = Operation.from_sequence( lib3.rotate_x(-np.pi/2).at(2), lib3.cphase(leakage=0.1).at(0, 2), lib3.cphase(leakage=0.25).at(2, 1), lib3.rotate_x(np.pi/2).at(2), lib3.rotate_x(np.pi).at(0), lib3.rotate_x(np.pi).at(1) ) zzc = zz.compile(bases_in=bases_in, bases_out=bases_out) assert len(zzc.operations) == 2 op1, ix1 = zzc.operations[0] op2, ix2 = zzc.operations[1] assert ix1 == (0, 2) assert ix2 == (1, 2) assert op1.bases_in[0] == bases_in[0] assert op2.bases_in[0] == bases_in[1] assert op1.bases_in[1] == bases_in[2] # Qubit 0 did not leak assert op1.bases_out[0] == bases_out[0].subbasis([0, 1, 3, 4]) # Qubit 1 leaked assert op2.bases_out[0] == bases_out[1].subbasis([0, 1, 2, 6]) # Qubit 2 is measured assert op2.bases_out[1] == bases_out[2] dm = random_density_matrix(3**3, seed=85) state1 = State.from_dm(dm, (b01, b01, b0)) state2 = State.from_dm(dm, (b01, b01, b0)) zz(state1, 0, 1, 2) zzc(state2, 0, 1, 2) # Compiled version still needs to be projected, so we can't compare # Pauli vectors, so we can to check only DM diagonals. assert np.allclose(state1.diagonal(), state2.diagonal())
def test_chain_apply(self): b = (bases.general(2), ) * 3 dm = random_hermitian_matrix(8, seed=93) pv1 = PauliVector.from_dm(dm, b) pv2 = PauliVector.from_dm(dm, b) # Some random gate sequence op_indices = [(lib2.rotate_x(np.pi / 2), (0, )), (lib2.rotate_y(0.3333), (1, )), (lib2.cphase(), (0, 2)), (lib2.cphase(), (1, 2)), (lib2.rotate_x(-np.pi / 2), (0, ))] for op, indices in op_indices: op(pv1, *indices), circuit = Operation.from_sequence(*(op.at(*ix) for op, ix in op_indices)) circuit(pv2, 0, 1, 2) assert np.all(pv1.to_pv() == pv2.to_pv())
def test_chain_apply(self): b = (bases.general(2), ) * 3 dm = random_density_matrix(8, seed=93) state1 = State.from_dm(dm, b) state2 = State.from_dm(dm, b) # Some random gate sequence op_indices = [(lib2.rotate_x(np.pi / 2), (0, )), (lib2.rotate_y(0.3333), (1, )), (lib2.cphase(), (0, 2)), (lib2.cphase(), (1, 2)), (lib2.rotate_x(-np.pi / 2), (0, ))] for op, indices in op_indices: op(state1, *indices), circuit = Operation.from_sequence(*(op.at(*ix) for op, ix in op_indices)) circuit(state2, 0, 1, 2) assert np.all(state1.to_pv() == state2.to_pv())
def test_chain_compile_three_qubit(self, d, lib): b = bases.general(d) b0 = b.subbasis([0]) chain0 = Operation.from_sequence( lib.rotate_x(0.5*np.pi).at(2), lib.cphase().at(0, 2), lib.cphase().at(1, 2), lib.rotate_x(-0.75*np.pi).at(2), lib.rotate_x(0.25*np.pi).at(2), ) chain1 = chain0.compile((b, b, b0), (b, b, b)) assert chain1.operations[0].indices == (0, 2) assert chain1.operations[0].operation.bases_in == (b, b0) assert chain1.operations[0].operation.bases_out[0] == b assert chain1.operations[1].indices == (1, 2) assert chain1.operations[1].operation.bases_in[0] == b assert chain1.operations[1].operation.bases_out[0] == b for label in '0', '1', 'X10', 'Y10': assert label in chain1.operations[1].operation.bases_out[1].labels
def test_ptm(self): # Some random gate sequence op_indices = [(lib2.rotate_x(np.pi / 2), (0, )), (lib2.rotate_y(0.3333), (1, )), (lib2.cphase(), (0, 2)), (lib2.cphase(), (1, 2)), (lib2.rotate_x(-np.pi / 2), (0, ))] circuit = Operation.from_sequence(*(op.at(*ix) for op, ix in op_indices)) b = (bases.general(2), ) * 3 ptm = circuit.ptm(b, b) assert isinstance(ptm, np.ndarray) op_3q = Operation.from_ptm(ptm, b) dm = random_hermitian_matrix(8, seed=93) state1 = PauliVector.from_dm(dm, b) state2 = PauliVector.from_dm(dm, b) circuit(state1, 0, 1, 2) op_3q(state2, 0, 1, 2) assert np.allclose(state1.to_pv(), state2.to_pv())
def test_chain_apply(self): b = (bases.general(2),) * 3 dm = random_density_matrix(8, seed=93) state1 = State.from_dm(dm, b) state2 = State.from_dm(dm, b) # Some random gate sequence op_indices = [(lib2.rotate_x(np.pi/2), (0,)), (lib2.rotate_y(0.3333), (1,)), (lib2.cphase(), (0, 2)), (lib2.cphase(), (1, 2)), (lib2.rotate_x(-np.pi/2), (0,))] for op, indices in op_indices: op(state1, *indices), circuit = Operation.from_sequence( *(op.at(*ix) for op, ix in op_indices)) circuit(state2, 0, 1, 2) assert np.all(state1.to_pv() == state2.to_pv())
def test_chain_compile_leaking(self): b = bases.general(3) chain0 = Operation.from_sequence( lib3.rotate_x(0.5*np.pi).at(2), lib3.cphase(leakage=0.1).at(0, 2), lib3.cphase(leakage=0.1).at(1, 2), lib3.rotate_x(-0.75*np.pi).at(2), lib3.rotate_x(0.25*np.pi).at(2), ) b0 = b.subbasis([0]) b01 = b.subbasis([0, 1]) b0134 = b.subbasis([0, 1, 3, 4]) chain1 = chain0.compile((b0, b0, b0134), (b, b, b)) # Ancilla is not leaking here anc_basis = chain1.operations[1].operation.bases_out[1] for label in anc_basis.labels: assert '2' not in label chain2 = chain0.compile((b01, b01, b0134), (b, b, b)) # Ancilla is leaking here anc_basis = chain2.operations[1].operation.bases_out[1] for label in '2', 'X20', 'Y20', 'X21', 'Y21': assert label in anc_basis.labels
def test_chain_merge_next(self, d, lib): b = bases.general(d) dm = random_density_matrix(d**2, seed=574) chain = Operation.from_sequence( lib.rotate_x(np.pi / 5).at(0), (lib.cphase(angle=3*np.pi/7, leakage=0.1) if d == 3 else lib.cphase(3*np.pi / 7)).at(0, 1), ) bases_full = (b, b) chain_c = chain.compile(bases_full, bases_full) assert len(chain.operations) == 2 assert isinstance(chain_c, _PTMOperation) state1 = State.from_dm(dm, bases_full) state2 = State.from_dm(dm, bases_full) chain(state1, 0, 1) chain_c(state2, 0, 1) assert state1.meas_prob(0) == approx(state2.meas_prob(0)) assert state1.meas_prob(1) == approx(state2.meas_prob(1))
def test_chain_merge_next(self, d, lib): b = bases.general(d) dm = random_hermitian_matrix(d**2, seed=574) chain = Operation.from_sequence( lib.rotate_x(np.pi / 5).at(0), (lib.cphase(angle=3 * np.pi / 7, leakage_rate=0.1) if d == 3 else lib.cphase(3 * np.pi / 7)).at(0, 1), ) bases_full = (b, b) chain_c = chain.compile(bases_full, bases_full) assert len(chain._units) == 2 assert isinstance(chain_c, PTMOperation) pv1 = PauliVector.from_dm(dm, bases_full) pv2 = PauliVector.from_dm(dm, bases_full) chain(pv1, 0, 1) chain_c(pv2, 0, 1) assert pv1.meas_prob(0) == approx(pv2.meas_prob(0)) assert pv1.meas_prob(1) == approx(pv2.meas_prob(1))
def test_chain_create(self): op1 = lib2.rotate_x() op2 = lib2.rotate_y() op3 = lib2.cnot() op_qutrit = lib3.rotate_x() circuit = Operation.from_sequence(op1.at(0), op2.at(0)) assert circuit.num_qubits == 1 assert len(circuit.operations) == 2 circuit = Operation.from_sequence(op1.at(1), op2.at(0)) assert circuit.num_qubits == 2 assert len(circuit.operations) == 2 with pytest.raises(ValueError, match=".* must form an ordered set .*"): Operation.from_sequence(op1.at(2), op2.at(0)) with pytest.raises(ValueError, match=".* must form an ordered set .*"): Operation.from_sequence(op1.at(1), op2.at(2)) with pytest.raises(ValueError, match="Hilbert dimensionality of op.*"): Operation.from_sequence(op1.at(0), op_qutrit.at(0)) circuit3q = Operation.from_sequence(op1.at(0), op2.at(1), op3.at(0, 1), op1.at(1), op2.at(0), op3.at(0, 2)) assert circuit3q.num_qubits == 3 assert len(circuit3q.operations) == 6 with pytest.raises(ValueError, match="Number of indices is not .*"): Operation.from_sequence(op1.at(0), op3.at(0)) with pytest.raises(ValueError, match="Number of indices is not .*"): circuit3q.at(0, 1) circuit4q = Operation.from_sequence(op3.at(0, 2), circuit3q.at(1, 2, 3)) assert len(circuit4q.operations) == 7 assert circuit4q.operations[0].indices == (0, 2) for o1, o2 in zip(circuit4q.operations[1:], circuit3q.operations): assert np.all(np.array(o1.indices) == np.array(o2.indices) + 1) circuit4q = Operation.from_sequence( circuit3q.at(2, 0, 3), op3.at(0, 1), op2.at(1)) assert len(circuit4q.operations) == 8 assert circuit4q.operations[0].indices == (2,) assert circuit4q.operations[1].indices == (0,) assert circuit4q.operations[2].indices == (2, 0)
def test_chain_create(self): op1 = lib2.rotate_x() op2 = lib2.rotate_y() op3 = lib2.cnot() op_qutrit = lib3.rotate_x() circuit = Operation.from_sequence(op1.at(0), op2.at(0)) assert circuit.num_qubits == 1 assert len(circuit.operations) == 2 circuit = Operation.from_sequence(op1.at(1), op2.at(0)) assert circuit.num_qubits == 2 assert len(circuit.operations) == 2 with pytest.raises(ValueError, match=".* must form an ordered set .*"): Operation.from_sequence(op1.at(2), op2.at(0)) with pytest.raises(ValueError, match=".* must form an ordered set .*"): Operation.from_sequence(op1.at(1), op2.at(2)) with pytest.raises(ValueError, match="Hilbert dimensionality of op.*"): Operation.from_sequence(op1.at(0), op_qutrit.at(0)) circuit3q = Operation.from_sequence(op1.at(0), op2.at(1), op3.at(0, 1), op1.at(1), op2.at(0), op3.at(0, 2)) assert circuit3q.num_qubits == 3 assert len(circuit3q.operations) == 6 with pytest.raises(ValueError, match="Number of indices is not .*"): Operation.from_sequence(op1.at(0), op3.at(0)) with pytest.raises(ValueError, match="Number of indices is not .*"): circuit3q.at(0, 1) circuit4q = Operation.from_sequence(op3.at(0, 2), circuit3q.at(1, 2, 3)) assert len(circuit4q.operations) == 7 assert circuit4q.operations[0].indices == (0, 2) for o1, o2 in zip(circuit4q.operations[1:], circuit3q.operations): assert np.all(np.array(o1.indices) == np.array(o2.indices) + 1) circuit4q = Operation.from_sequence(circuit3q.at(2, 0, 3), op3.at(0, 1), op2.at(1)) assert len(circuit4q.operations) == 8 assert circuit4q.operations[0].indices == (2, ) assert circuit4q.operations[1].indices == (0, ) assert circuit4q.operations[2].indices == (2, 0) Operation.from_sequence(circuit3q.at(0, 1, 2), Operation.from_sequence(op1, op2).at(1))
def amp_phase_damping(damp_rate, deph_rate): amp_damp = amp_damping(damp_rate) phase_damp = phase_damping(deph_rate) return Operation.from_sequence(amp_damp.at(0), phase_damp.at(0))
def _filter_wait_placeholders(operation): return Operation.from_sequence([ unit for unit in operation.units() if not isinstance(unit.operation, WaitPlaceholder) ])
def cphase(angle=np.pi, *, integrate_idling=False, model='legacy', **kwargs): """ Parameters ---------- angle : float Conditional phase of a CPhase gate, default is :math:`\\pi`. integrate_idling : bool Whether to return model : str Error model (currently only 'legacy' and 'NetZero' is implemented). **kwargs Parameters for the error model. Returns ------- Operation Resulting CPhase operation. First qubit is static (low-frequency) qubit, """ def p(name): return kwargs.get(name, default_cphase_params[name]) for param in kwargs.keys(): if param not in default_cphase_params.keys(): raise ValueError('Unknown model parameter: {}'.format(param)) int_point_time = p('int_time') - (4 * p('rise_time')) if np.isfinite(p('q1_t2')) and np.isfinite(p('q1_t2_int')): rise_t2 = (p('q1_t2') + p('q1_t2_int')) / 2 else: rise_t2 = np.inf int_time = p('int_time') leakage_rate = p('leakage_rate') qstatic_deviation = int_time * np.pi * \ p('sensitivity') * (p('quasistatic_flux') ** 2) qstatic_interf_leakage = (0.5 - (2 * leakage_rate)) * \ (1 - np.cos(1.5 * qstatic_deviation)) phase_corr_error = p('phase_corr_error') rot_angle = angle + (1.5 * qstatic_deviation) + (2 * phase_corr_error) if model.lower() == 'legacy': cz_op = _cphase_legacy(angle, leakage_rate) elif model.lower() == 'netzero': ideal_unitary = expm( 1j * _ideal_generator(phase_10=phase_corr_error, phase_01=phase_corr_error + qstatic_deviation, phase_11=rot_angle, phase_02=rot_angle, phase_12=p('phase_diff_02_12') - rot_angle, phase_20=0, phase_21=p('phase_diff_20_21'), phase_22=p('phase_22'))) noisy_unitary = expm(1j * _exchange_generator( leakage=4 * leakage_rate + qstatic_interf_leakage, leakage_phase=p('leakage_phase'), leakage_mobility_rate=p('leakage_mobility_rate'), leakage_mobility_phase=p('leakage_mobility_phase'), )) cz_unitary = ideal_unitary @ noisy_unitary if not verify_kraus_unitarity(cz_unitary): raise RuntimeError("CPhase gate is not unitary, " "verify provided parameters.") cz_op = Operation.from_kraus(cz_unitary, bases2_default) else: raise ValueError('Unknown CZ model: {}'.format(model)) if integrate_idling: q0_t1 = p('q0_t1') q0_t2 = p('q0_t2') q0_anharmonicity = p('q0_anharmonicity') q1_t1 = p('q1_t1') q1_t2 = p('q1_t2') q1_t2_int = p('q1_t2_int') q1_anharmonicity = p('q1_anharmonicity') rise_time = p('rise_time') phase_corr_time = p('phase_corr_time') return Operation.from_sequence( idle(int_time / 2, q0_t1, q0_t2, q0_anharmonicity).at(0), idle(rise_time, q1_t1, rise_t2, q1_anharmonicity).at(1), idle(int_point_time / 2, q1_t1, q1_t2_int, q1_anharmonicity).at(1), idle(rise_time, q1_t1, rise_t2, q1_anharmonicity).at(1), cz_op.at(0, 1), idle(rise_time, q1_t1, rise_t2, q1_anharmonicity).at(1), idle(int_point_time / 2, q1_t1, q1_t2_int, q1_anharmonicity).at(1), idle(rise_time, q1_t1, rise_t2, q1_anharmonicity).at(1), idle(int_time / 2, q0_t1, q0_t2, q0_anharmonicity).at(0), idle(phase_corr_time, q0_t1, q0_t2, q0_anharmonicity).at(0), idle(phase_corr_time, q1_t1, q1_t2, q1_anharmonicity).at(1)) else: return cz_op
def cnot_like(angle_cphase, angle_rotate): return Operation.from_sequence( lib.rotate_y(angle_rotate).at(1), lib.cphase(angle_cphase).at(0, 1), lib.rotate_y(-angle_rotate).at(1))